djechlin djechlin - 4 months ago 6x
Java Question

Why doesn't catching Exception catch RuntimeException?

This is very odd to me.

inherits from
, which inherits from

catch(Exception exc) { /* won't catch RuntimeException */


catch(Throwable exc) { /* will catch RuntimeException */

I know
is special in that it's unchecked. But to my understanding that applies just to whether exceptions have to be declared, not whether they are caught. And even then, I don't know why this logic would break on catching Throwable.

This is pretty relevant to me since I have a situation where RuntimeExceptions can be thrown in a terminal operation. I'm not sure the name for this pattern, but something like, my class
takes an array of
. The code looks like this:

for(Callback cb : callbacks) {
try {;
catch(Exception exc) {
logger.error("Error in callback: ", exc);

So this is a case where something like an OOME needs to fly through, because if one of these callbacks consumes all machine memory, that sure as heck is going to affect the running of the other ones. But a
? Or an
? Those will affect the callback but won't prevent the others from running.

Also, this is a bit of an enterprise design. Different programmers or teams can add callbacks to process the item, but they should be isolated from each other. This means, as the programmer responsible for insulating these callbacks from each other, I shouldn't rely on them to make sure errors don't slip through. Catching
should be about the right line, but it isn't because
slips through. So my more general question is: what's a good pattern here? Just
catch(Exception | RuntimeException exc)
, which I believe is a syntax error because of the inheritance?


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);


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?