try{} catch{} finally{} Statement considerations Summary

Source: Internet
Author: User
Tags finally block

try{} catch{} finally{} statement considerations:
  • If there is a catch{} statement match, the other catch{} branches will not execute
  • try{} If there is a return, will also first execute the finally{} inside the statement, and then return
  • If try{} and finally{} are both with the return statement, the return inside the try{} will not be executed
  • finally{} What circumstances will not be executed?
    • Return before try{}
    • In the case of try{} there is System.exit ()
    • The case that the thread was interrupted/killed
    • Crash, power outage
  • If there is an exception in finally{}, what are the consequences?
    • Will overwrite the exception inside the try{}, causing the exception inside the try{} to not be captured
    • The code behind the finally{} exception will not be executed
    • Will be captured by the caller
  • If there is an exception in finally{}, how to solve it?
    • Left to the caller to resolve
    • After capturing it in finally{}, write it into the log.

Reference article: "The Java Tutorials":

The finally Block

The finally block always executes when the try block exits. This ensures, the finally block is executed even if an unexpected exception occurs. But finally was useful for more than just exception handling-it allows the programmer to avoid have cleanup code accide Ntally bypassed by a return,continue, or breaks. Putting cleanup code in a finally block was always a good practice, and even when no exceptions was anticipated.

Note:if the JVM exits while the try or catch code is being executed and then the finally block could not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block could not execute even t Hough the application as a whole continues.


try{} catch{} finally{} Statement considerations Summary

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.