Java-Will the finally execute if there is a return statement in the try? Before or after the return?

Source: Internet
Author: User

Executes after the return statement executes before the method return action, and if finally there is a return statement, the method returns as a finally return, and if there is no return statement in Finally, Then this method returns as the return of the try.P:The return value of the method is based on the result of the last execution of the return statement, and once the return value is determined, it does not affect subsequent code execution.Test procedure:public static void Main (string[] args) {System.out.println (test ()); System.out.println ("Finish..");  }private static int Test () {int b = 20;   try {System.out.println ("try block");  return B + = 80;   } finally {System.out.println ("finally block");   if (b >) {System.out.println ("b =" + B); }//If this line cancels the comment, seeInput 2。    return 1; If this line cancels the comment, seeOutput 3。 B--;  The return value of the method is determined before finally (it can be understood that each method has a memory space to hold the return value), and modifying B here does not affect the return value. } }Output:Try blockfinally blockb = 100100finish..Output 2:Try blockfinally blockb = 1001finish..Output 3:Try blockfinally blockb = 100100finish..

Java-Will the finally execute if there is a return statement in the try? Before or after the return?

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.