Why return, continue, or break is not allowed in finally Blocks

Source: Internet
Author: User
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
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.