Can I catch multiple exception types in one catch block in EK9?
← Control Flow · Ref: Q932
Each EK9 catch block handles exactly ONE exception type. There is no multi-catch syntax.
SINGLE CATCH
try riskyWork() catch -> ex as Exception stderr.println(`Failed: ${ex}`)
The catch uses -> on its own line to declare the incoming exception. This follows EK9's data-flow convention: -> means data flowing IN.
WHY ONE TYPE PER CATCH
Multi-catch (Java's catch (A | B e)) creates ambiguity about the exception's actual type inside the handler. EK9 enforces clarity: you know the exact type you are handling.
HANDLING DIFFERENT TYPES
Use separate try/catch blocks or catch the base Exception type:
try riskyWork() catch -> ex as Exception stderr.println(`Caught: ${ex}`)
Since all EK9 exceptions descend from Exception, catching Exception handles everything.
See Q902 for try/catch basics. See Q934 for try/finally. See Q130 for control flow overview.
Example
defines module qa.controlflow.singlecatch defines function safeDivision() as pure -> numerator as Integer denominator as Integer <- rtn as Float: #^ numerator / #^ denominator defines program SingleCatchDemo() stdout <- Stdout() stderr <- Stderr() //Single exception type per catch try outcome <- safeDivision(42, 7) stdout.println(`42 / 7 = ${outcome}`) catch -> ex as Exception stderr.println(`Division failed: ${ex}`) //Catching the base Exception covers all exception types try second <- safeDivision(100, 5) stdout.println(`100 / 5 = ${second}`) catch -> ex as Exception stderr.println(`Error: ${ex}`) finally stdout.println("Division attempt complete")
Common mistakes
E07850 — An EK9 catch block's arrow may declare only ONE exception variable (E07850). Declaring two (ex1, ex2) in a single catch is rejected. Use one catch block, catching Exception to handle all types. See ek9 -h E07850 for details.
Incorrect:
catch -> ex1 as Exception ex2 as Exception stderr.println(`Failed: ${ex1}`)
Correct:
catch -> ex as Exception stderr.println(`Division failed: ${ex}`)
Other ways to ask this
- Does EK9 support multi-catch like Java?
- What triggers E07850 in EK9 catch blocks?
- How many exception types can a catch handle in EK9?
Coming from another language?
Java: catch (IOException | SQLException e) multi-catch since Java 7. Python: except (TypeError, ValueError) as e. C#: catch (Exception e) when — filter pattern. EK9: one type per catch block, no multi-catch.
Keywords: multi-catch, exception, type, catch, try, handler, E07850, single