well actually
well actually

Reputation: 12370

Java: Hypothetical question about finally block

What happens if you throw an error in a finally block? Does it get handled in one of the corresponding catch clauses?

Upvotes: 0

Views: 514

Answers (7)

Harry Joy
Harry Joy

Reputation: 59660

It will not handle exception until it is caught in finally block it self.

public static void main(String[] args) throws Exception {
        try {
            System.out.println("In try");
        } catch (Exception e) {
            System.out.println("In catch");
        } finally{
            throw new Exception();
        }

    }

Above code will throw exception but if you do as follows it will work:

public static void main(String[] args){
        try {
            System.out.println("In try");
        } catch (Exception e) {
            System.out.println("In catch");
        } finally{
             try{
                     throw new Exception();
                 }catch(Exception e){}
        }

    }

Upvotes: 2

Thomas
Thomas

Reputation: 88707

The order of execution is normally directly indicated by the order of statements: 1. try, 2. catch exceptions in the specified order (only one catch is executed), 3. finally.

So when the finally block is executed (note that this is always the case, even in the case of a return statement or exception being thrown in the try or catch blocks) the execution of try statement is in its last phase and thus it cannot catch further throwables. As already pointed out, the exception has to be handled in a location further down the stack (or up, depends on the view point ;) ).

Upvotes: 0

Aleadam
Aleadam

Reputation: 40391

You need to include try-catch blocks inside the finally or catch blocks.

e.g.:

try {
    // your code here
} finally {
    try {
        // if the code in finally can throw another exception, you need to catch it inside it
    } catch (Exception e) {
       // probably not much to do besides telling why it failed
    }
} catch (Exception e) {
    try {
        // your error handling routine here
    } catch (Exception e) {
       // probably not much to do besides telling why it failed
    }
}

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500525

No, a catch block can only catch exceptions thrown within the corresponding try block - not a finally block. (Of course, if that finally block is within another try block, the catch clauses for that try block are still used.)

The relevant section in the JLS is 14.20.2. Each of the flows listed there has something like this:

If the finally block completes abruptly for any reason, then the try statement completes abruptly for the same reason.

In other words, there's no attempt for any catch clauses associated with the finally block to handle the exception.

Upvotes: 1

Dave
Dave

Reputation: 1567

No it doesn't. You will have to handle with it IN the finally block or define a proper throw declaration in the method description.

Upvotes: 1

Tommy
Tommy

Reputation: 1267

Only if you put another try-catch block in the finally block. Otherwise it's an error like any other.

Upvotes: 6

squawknull
squawknull

Reputation: 5192

Nope. It would be caught by a catch where then entire try/catch/finally was nested within another try/catch. The exception would otherwise be thrown out of the function, and would be handled by the caller of the function.

Upvotes: 1

Related Questions