E07380: return not possible, as instructions only result in an Exception

Category: E07xxx Full resolution · Reported in phase: FULL_RESOLUTION · Classification: RETURN_UNREACHABLE · All error codes

What went wrong

A function, method or dynamic function declares a returning value ('<- rtn ...'), but every path through its body throws an exception, so the value can never be returned. (EK9 has no 'return' statement: the returning variable is the result.)

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 1318 (Why does EK9 say a return is not possible when all bra...)

How to fix it

  1. If nothing is ever returned, remove the returning declaration (a dynamic function then needs a super that returns nothing, such as Consumer)
  2. Or add a path that does not throw and gives the returning variable a value

The [EXAMPLE] section below shows dynamic functions whose every path throws, and one where a branch gives the result a value.

Common causes

  • A returning declaration left after refactoring
  • Misunderstanding control flow
  • A dynamic function whose super returns a value (e.g. 'is Supplier of String') has a body that can only throw - there is no path that returns. A super that returns nothing (e.g. Consumer) may only throw.

See also

  • E07380: unreachable return after throws (this error)
  • E07370: unreachable code after throw

Examples

Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.

Fails with E07380: return not possible, as instructions only result in an Exception

#!ek9
defines module error.e07380.bad

  defines function

    blockBodyOnlyThrows()
      <- rtn as Boolean?
      supplier <- () is Supplier of String as pure function
        throw Exception("always")
      rtn: supplier?

    singleStatementBodyOnlyThrows()
      <- rtn as Boolean?
      supplier <- () is Supplier of String as pure function (throw Exception("always"))
      rtn: supplier?

    everyBranchThrows()
      <- rtn as Boolean?
      describer <- () is Function of (Integer, String) as pure function
        if t > 0
          throw Exception("positive")
        else
          throw Exception("not positive")
      rtn: describer?

    oneBranchReturnsIsFine()
      <- rtn as Boolean?
      describer <- () is Function of (Integer, String) as pure function
        if t > 0
          throw Exception("positive")
        else
          r: "not positive"
      rtn: describer?

    voidSuperMayOnlyThrow()
      <- rtn as Boolean?
      consumer <- () is Consumer of String as pure function
        throw Exception("always")
      rtn: consumer?

Fixed: Give at least one path a value to return

#!ek9
defines module error.e07380.good

  defines program

    Describe()
      stdout <- Stdout()

      //One branch gives the result a value, so it can be returned
      describer <- () is Function of (Integer, String) as pure function
        if t > 0
          throw Exception("positive")
        else
          r: "not positive"

      stdout.println(describer(0))

Questions and answers

These Q&A entries show E07380 and its fix:

Found a problem?

If EK9 reported E07380 for a program you think is valid, or did not report it where it should have, use the compiler form with the smallest program that shows it. If this page is wrong or unclear, use the documentation form. Both are at github.com/ek9lang/ek9-issues.