What happens if I write code after a throw statement in EK9?

← Control Flow · Ref: Q933

EK9 detects unreachable code and flags it as a compile error. Code placed after an unconditional throw can never execute.

UNREACHABLE CODE DETECTION

The compiler performs flow analysis. When every path through a block ends with throw, any subsequent code is dead:

  try
    processData()
  catch
    -> ex as Exception
    stderr.println(`Error: ${ex}`)

EK9 treats unreachable statements as errors, not warnings. Dead code indicates a logic flaw that must be fixed.

PROPER PATTERN

Place all meaningful work before the throw. If conditional logic determines whether to throw, the compiler tracks each branch independently.

CONDITIONAL THROW IS FINE

  if not isValid
    throw Exception("invalid")
  //Code here IS reachable because the throw is conditional
  processValid()

See Q902 for try/catch basics. See Q932 for single catch type. See Q144 for control flow philosophy.

Example

defines module qa.controlflow.unreachable

  defines function

    validateAge() as pure
      -> age as Integer
      <- rtn as Boolean: age >= 0

  defines program

    UnreachableDemo()
      stdout <- Stdout()
      stderr <- Stderr()

      age <- 25

      //Conditional throw — code after is reachable
      if not validateAge(age)
        throw Exception("Invalid age")

      //This line is reachable because throw is conditional
      stdout.println(`Valid age: ${age}`)

      //Proper try/catch with no dead code
      try
        if not validateAge(-1)
          throw Exception("Negative age")
        stdout.println("Age validated")
      catch
        -> ex as Exception
        stderr.println(`Caught: ${ex}`)

Common mistakes

E07370 — Code placed after an unconditional throw can never execute and is rejected as unreachable. See ek9 -h E07370 for details.

Incorrect:

        throw Exception("Negative age")
        stdout.println("Age validated")

Correct:

        if not validateAge(-1)
          throw Exception("Negative age")
        stdout.println("Age validated")
Other ways to ask this
  • What triggers E07370 or E07380 in EK9?
  • Does EK9 detect unreachable code?
  • Can I have statements after throw in EK9?

Coming from another language?

Java: unreachable statement is a compile error. C#: unreachable code warning (CS0162). Python: no unreachable detection. Rust: warns on unreachable code. Go: no unreachable detection. EK9: compile error for unreachable code after throw.

Keywords: throw, dead, unreachable, E07370, E07380, flow, code, analysis