DRAFT
AnyClassException
Misconception:
Any object of a class can be thrown as an exception.
Incorrect
Any class can be an exception class
Correct
Exceptions are subtypes of Throwable
CorrectionHere is what's right.
Here is what's right.
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);
}
OriginWhere could this misconception come from?
Where could this misconception come from?
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);
}