E07850: only a single Exception is supported
What went wrong
A catch declares more than one exception parameter. A try has exactly ONE catch, and that catch takes exactly one parameter (the '->' line under 'catch').
Worked examples
Compilable examples demonstrating this error and the correct fix:
- ek9 -q 932 (Can I catch multiple exception types in one catch bloc...)
The [EXAMPLE] section below shows a catch with two parameters, then the same catch taking a single Exception.
Rationale
One catch with one parameter means every exception leaving the try is handled in one place, by one name. There is no list of catches to fall through and no multi-catch whose variable has a type the reader has to work out.
How to fix it
- Declare a single parameter, typed as the nearest super type of every exception the try can throw (Exception, or a base exception type of your own)
- If the reaction must differ by exception type, put it in the exception types themselves (a method each one overrides) and call that method in the catch
Common causes
- Habit from Java 7+ multi-catch ('catch (IOException | SQLException e)')
- Expecting one catch block per exception type, as in Java or C# - EK9 has one catch
See also
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E07850: only a single Exception is supported
#!ek9 defines module error.e07850.bad defines function exceptionFunction() -> count as Integer <- rtn as Integer: count if count < 0 throw Exception("negative") tryOnce() stdout <- Stdout() try stdout.println($exceptionFunction(-1)) catch //A catch takes one exception -> first as Exception second as Exception stdout.println(first.reason() + second.reason())
Fixed: Catch a single exception
#!ek9 defines module error.e07850.good defines function exceptionFunction() -> count as Integer <- rtn as Integer: count if count < 0 throw Exception("negative") tryOnce() stdout <- Stdout() try stdout.println($exceptionFunction(-1)) catch -> ex as Exception stdout.println(ex.reason())
Questions and answers
These Q&A entries show E07850 and its fix: