In Java, try Catch finally executes

Source: Internet
Author: User

Directly on the code instance:

public static void Main (string[] args) {

System.out.println (Test1 ());
}
static int test1 () {
int a=0;
try {
return Ret1 ();
} catch (Exception e) {
Todo:handle exception
}finally {
return Ret2 ();
}
}
static int Ret1 () {
System.out.println ("Return1");
return 1;
}
static int Ret2 () {
System.out.println ("return2");
return 2;
}

Output Result:

Return1
Return2
2

Results analysis: When the Main method is executed, reversed to the Test1 () method, no exception occurs, executes the contents of the try, call the Ret1 () method because there is return, so jump directly. After execution of the contents of the try is done, because there is no exception so do not go catch, execute finally inside the method, the last return returned to execute test1. Returns to the main method. All output results.

In Java, try Catch finally executes

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.