E04030: type must be of Exception type
What went wrong
Only types that extend Exception can be used with throw/catch. You're trying to throw or catch a type that isn't an exception.
Worked examples
Compilable examples demonstrating this error and the correct fix:
- ek9 -q 136 (How do I throw exceptions in EK9?)
- ek9 -q 134 (How do I use try/catch to handle exceptions in EK9?)
- ek9 -q 138 (How do I catch specific exception types in EK9?)
- ek9 -q 135 (How does try/catch/finally work in EK9?)
- ek9 -q 305 (What is the difference between require, assert, and throw?)
- ek9 -q 1434 (What is a Panic in EK9 and when does the language raise one?)
[BUILT-IN EXCEPTIONS]
- Exception (base type)
- Various system exceptions for I/O, parsing, etc.
Panic is not an exception
Panic is raised only by the language (a failed require, a constrained-type value out of range, an unset for-range bound). It is not an Exception, so 'catch -> ex as Panic' is this error. User code cannot create a Panic at all: 'Panic(...)', and so 'throw Panic(...)', is E50060. A Panic always ends the program with exit status 1. To end with a status you choose, throw an Exception carrying it, Exception("Settings missing", 3), and do not catch it.
How to fix it
- Add 'extends Exception' to your error type
- Or use a built-in exception type
Common causes
- Forgetting 'extends Exception' on custom error type
- Using a record or enum where exception needed
- Typo causing wrong type to be thrown
- Throwing or catching Panic, which only the language raises
- Throwing a message String (ex.reason()) instead of the exception itself
See also
- E07850: only a single Exception per catch block
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E04030: type must be of Exception type
#!ek9 defines module error.e04030.bad defines function validated() -> amount as Integer <- rtn as Integer: amount if amount < 0 throw String("The amount cannot be negative")
Fixed: Throw an Exception (or a type that extends it), carrying the message
#!ek9 defines module error.e04030.good defines function validated() -> amount as Integer <- rtn as Integer: amount if amount < 0 throw Exception("The amount cannot be negative")
Questions and answers
These Q&A entries show E04030 and its fix:
- Q134: How do I use try/catch to handle exceptions in EK9?
- Q135: How does try/catch/finally work in EK9?
- Q136: How do I throw exceptions in EK9?
- Q138: How do I catch specific exception types in EK9?
- Q305: What is the difference between require, assert, and throw?
- Q306: How do I test that code throws an exception in EK9?
- Q902: How do try/catch/finally work in EK9?
- Q1434: What is a Panic in EK9 and when does the language raise one?
- Q1436: Should I catch an Exception that carries an exit code, or let it end the program?
Found a problem?
If EK9 reported E04030 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.