74

This is very odd to me. RuntimeException inherits from Exception, which inherits from Throwable.

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

but

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

I know RuntimeException 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 RuntimeException can be thrown in a terminal operation. I'm not sure the name for this pattern, but something like, my class EmailRoller takes an array of Callbacks. The code looks like this:

for(Callback cb : callbacks) {
    try {
        cb.call(item);
    }
    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 NullPointerException? Or an IndexOutOfBoundsException? 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 Exception should be about the right line, but it isn't because RuntimeException 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?

djechlin
  • 59,258
  • 35
  • 162
  • 290
  • You should edit the question title - if someone only looks at the title, they will get the idea that catching exception doesn't catch runtime exception. – Alex Ciminian May 07 '21 at 10:23
  • 2
    @AlexCiminian good point. Here's to another 8 years of informative questions! – djechlin May 07 '21 at 10:25

5 Answers5

174

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?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    Reread my code. That's correct. My insulation layer was buggy, since the runtime exception came from an `if(results.foo.bar()) { /* apply callback */ }`, which was an NPE. – djechlin Oct 03 '13 at 16:16
  • 9
    @djechlin: Your post doesn't *show* `if (results.foo.bar())` - it's very unclear what you're actually asking at this point... – Jon Skeet Oct 03 '13 at 16:21
  • Check my post below, Exception isn't catching runtime exception. – Kabs Apr 05 '18 at 08:04
36
catch (Exception ex) { ... }

WILL catch RuntimeException.

Whatever you put in catch block will be caught as well as the subclasses of it.

Jan Zyka
  • 17,460
  • 16
  • 70
  • 118
14

Catching Exception will catch a RuntimeException

cmd
  • 11,622
  • 7
  • 51
  • 61
3

I faced similar scenario. It was happening because classA's initilization was dependent on classB's initialization. When classB's static block faced runtime exception, classB was not initialized. Because of this, classB did not throw any exception and classA's initialization failed too.

class A{//this class will never be initialized because class B won't intialize
  static{
    try{
      classB.someStaticMethod();
    }catch(Exception e){
      sysout("This comment will never be printed");
    }
 }
}

class B{//this class will never be initialized
 static{
    int i = 1/0;//throw run time exception 
 }

 public static void someStaticMethod(){}
}

And yes...catching Exception will catch run time exceptions as well.

rajya vardhan
  • 1,121
  • 4
  • 16
  • 29
-2
class Test extends Thread
{
    public void run(){  
        try{  
            Thread.sleep(10000);  
        }catch(InterruptedException e){  
            System.out.println("test1");
            throw new RuntimeException("Thread interrupted..."+e);  
        }  

    }  

    public static void main(String args[]){  
        Test t1=new Test1();  
        t1.start();  
        try{  
            t1.interrupt();  
        }catch(Exception e){
            System.out.println("test2");
            System.out.println("Exception handled "+e);
        }  

    }  
}

Its output doesn't contain test2 , so its not handling runtime exception. @jon skeet, @Jan Zyka

Kabs
  • 187
  • 1
  • 6
  • 5
    The thread which is throwing the RuntimeException isn't the thread that is trying to catch the exception. That has nothing to do with whether it's RuntimeException or not. Your `main` method has successfully managed to interrupt the thread, so *that* `try` block has nothing to catch. There's then separately a `RuntimeException` which is thrown up the stack and not caught by anything (in user code) because there's nothing *in* the stack to catch it. – Jon Skeet Apr 05 '18 at 11:15
  • Tho answer is flaw but still enlightening – Stan Aug 05 '22 at 00:14