DRAFT
Observed
Any class can be an exception class
Exceptions are subtypes of Throwable
Only instances of java.lang.Throwable
and its subclasses can be thrown as exceptions.
try {
throw new Throwable();
} catch (Throwable ex) {
System.out.println(ex);
}
It’s not possible to throw
values of other types (neither primitive nor reference).
The following is illegal and will not compile:
try {
throw "Oh My!"; // type error -- throw requires a Throwable
} catch (String ex) {
System.out.println(ex);
}
A reason for this Java misconception may be that
a student already knows a language like JavaScript
that allows throw
ing values of any type:
try {
throw "Oh My!"; // ok in JavaScript
} catch (ex) {
console.log("Caught " + ex);
}