Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to explain whether Exception will catch RuntimeException?

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?

like image 992
djechlin Avatar asked Oct 03 '13 16:10

djechlin


People also ask

Does exception catch RuntimeException?

Catching Exception or ThrowableCatching Exception will catch both checked and runtime exceptions. Runtime exceptions represent problems that are a direct result of a programming problem, and as such shouldn't be caught since it can't be reasonably expected to recover from them or handle them.

Is it a good practice to catch a RuntimeException?

Additionally, catching RuntimeException is considered as a bad practice. And, thus, throwing Generic Exceptions/Throwable would lead the developer to catch the exception at a later stage which would eventually lead to further code smells.

What happens if a runtime exception occurs?

The Runtime Exception is the parent class in all exceptions of the Java programming language that are expected to crash or break down the program or application when they occur. Unlike exceptions that are not considered as Runtime Exceptions, Runtime Exceptions are never checked.


2 Answers

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?

like image 181
Jon Skeet Avatar answered Oct 18 '22 03:10

Jon Skeet


catch (Exception ex) { ... } 

WILL catch RuntimeException.

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

like image 45
Jan Zyka Avatar answered Oct 18 '22 03:10

Jan Zyka