Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to detect if an exception occurred before I entered a finally block?

Tags:

In Java, is there an elegant way to detect if an exception occurred prior to running the finally block? When dealing with "close()" statements, it's common to need exception handling within the finally block. Ideally, we'd want to maintain both exceptions and propagate them up (as both of them may contain useful information). The only way I can think of to do this is to have a variable outside the try-catch-finally scope to save a reference to a thrown exception. Then propagate the "saved" exception up with any that occur in the finally block.

Is there a more elegant way of doing this? Perhaps an API call that will reveal this?

Here's some rough code of what I'm talking about:

Throwable t = null;  try {       stream.write(buffer);  } catch(IOException e) {     t = e;   //Need to save this exception for finally     throw e; } finally {        try {        stream.close();   //may throw exception    } catch(IOException e) {       //Is there something better than saving the exception from the exception block?       if(t!=null) {          //propagate the read exception as the "cause"--not great, but you see what I mean.          throw new IOException("Could not close in finally block: " + e.getMessage(),t);       } else {          throw e;  //just pass it up       }        }//end close } 

Obviously, there are a number of other similar kludges that might involve saving the exception as an member variable, returning it from a method, etc... but I'm looking for something a bit more elegant.

Maybe something like Thread.getPendingException() or something similar? For that matter, is there an elegant solution in other languages?

This question actually spawned from comments in another question that raised an interesting question.

like image 483
James Schek Avatar asked Oct 08 '08 20:10

James Schek


People also ask

What happens if there is an exception inside finally block?

The "finally" block execution stops at the point where the exception is thrown. Irrespective of whether there is an exception or not "finally" block is guaranteed to execute. Then the original exception that occurred in the try block is lost.

Is finally block executed if there is no exception?

A finally block always executes, regardless of whether an exception is thrown.

Can exception be thrown in finally block?

Methods invoked from within a finally block can throw an exception. Failure to catch and handle such exceptions results in the abrupt termination of the entire try block.

How do I get exceptions in finally block?

inside try block catch : exception handled. finally : i execute always. In this case, the program throws an exception but not handled by catch so finally block execute after the try block and after the execution of finally block program terminate abnormally, But finally block execute fine.


1 Answers

Your idea about setting a variable outside the scope of the try/catch/finally is correct.

There cannot be more than one exception propagating at once.

like image 69
Jeremy Ross Avatar answered Oct 17 '22 02:10

Jeremy Ross