开发者

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 c开发者_开发百科atch clauses?


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


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
    }
}


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){}
        }

    }


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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜