The only way to exit a try block without executing the finally clause is to call the system. Exit () method.
If a return, continue, or break statement leaves the try block, the finally statement will be executed before the control is transferred to its new target code.
That is to say, if return, continue, or break is used in the Finally block, the thrown exception will be eaten.
Package test;
Public class trytest {
Public static void main (string [] ARGs ){
Try {
System. Out. println (trytest. Test (); // The returned result is true without any exception.
} Catch (exception e ){
System. Out. println ("exception from main ");
E. printstacktrace ();
}
}
Public static Boolean test () throws exception {
Try {
Throw new exception ("Something error"); // 1. Throw an exception
} Catch (exception e) {// 2. Catch the exception match (Declaration class or parent class) and enter the control block
System. Out. println ("exception from e"); // 3. Print
Return false; // 4. Transfer the control to the Finally block before return, and return after execution
} Finally {
Return true; // 5. Transfer control, return directly, eat an exception
}
}
}