Why doesn't catching Exception catch RuntimeException?
The premise of the question is flawed, because catching Exception
does catch RuntimeException
. Demo code:
public class Test {
public static void main(String[] args) {
try {
throw new RuntimeException("Bang");
} catch (Exception e) {
System.out.println("I caught: " + e);
}
}
}
Output:
I caught: java.lang.RuntimeException: Bang
Your loop will have problems if:
callbacks
is null- anything modifies
callbacks
while the loop is executing (if it were a collection rather than an array)
Perhaps that's what you're seeing?
catch (Exception ex) { ... }
WILL catch RuntimeException.
Whatever you put in catch block will be caught as well as the subclasses of it.
Catching Exception
will catch a RuntimeException