Finally block reasons not permitted return, continue, or break the

Finally block reasons not permitted return, continue, or break the

The only way a try block can not execute finally clauses will be able to withdraw is achieved by calling System.exit () method.

If the control as a return, continue, or break away from the try block of instructions, will then finally block before it is transferred to the new object code executable in the control.

This means that if the use of return in the finally block, continue, or break, it will eat the thrown exception.

Similarly: If you use a throw or have code in the finally block throws an exception in question, will be eaten raw exceptions, but because at this time able to throw new Exception can be tracked, so it is not a problem.

package test;

public class TryTest {
public static void main(String[] args) {
try {
TryTest tryTest = new TryTest();
System.out.println(tryTest.test());
} catch (Exception e) {
e.printStackTrace();
}
}

Test Boolean public () throws Exception {
the try {
the throw new new Exception ( "Something error"); // 1. thrown
} catch (Exception e) {// 2. uncaught exception matching (or its parent class declaration) , access control block
throw e; // 3. throw before control is transferred to the finally block, before executing the return
} finally {
return to true; // 4. control transfers directly back, no return catch block abnormal eaten
}
}
}

Print the result is true, main method not catch exceptions.

Published 87 original articles · won praise 233 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_43518645/article/details/105374980