About try catch finally, trycatchfinally

Source: Internet
Author: User
Tags try catch

About try catch finally, trycatchfinally
I. try catch finally

First, when should we use try/catch and finally? -- If the code executed is known to have exceptions

Try {// The Code executed here jumps to catch immediately if an exception occurs .}

Catch {// The code here will not be executed unless an exception occurs during code execution in try}

Finally {// the code in finally will be executed no matter whether there is any exception. It can be understood that as long as a try or catch is executed, finally} will be executed}

Ii. try catch finally execution sequence (no return)

1. Let's look at the code section.

1 public class Test01 {2 public static void main (String [] args) {3 int I = 1; 4 test01 (I ); 5} 6 7 public static void test01 (int I) {8 try {9 I ++; 10 System. out. println ("try -----" + I); 11 I = 1/0; // creates an abnormal sentence 12 I ++; 13 System. out. println ("try2 -----" + I); 14} catch (Exception e) {15 I ++; 16 System. out. println ("catch -----" + I); 17} finally {18 I = 5; 19 System. out. println ("finally -----" + I); 20} 21} 22}

2. the console output result is:

      

 

3. Conclusion: The execution sequence is as follows:Try {} catch {} finally {};

That is, execute try {} (try ----- 2) first. If an exception (I = I/0;) occurs in try, skip to execute catch (catch ----- 3 ), the finally (finally ----- 5) will be executed no matter whether an exception occurs );

(If there is no exception in try, catch will not be executed, so it will not be demonstrated here)

Iii. try catch finally execution sequence (with return). There are two cases:

First, return is after try catch finally.

1. Let's look at the code section.

 1 public class Test01 { 2     public static void main(String[] args) { 3         int i = 1; 4         i=test01(i); 5         System.out.println("main--------"+i); 6     } 7      8     public static int test01(int i) { 9         try {10             i++;11             System.out.println("try-----"+i);12         } catch (Exception e) {13             i++;14             System.out.println("catch----"+i);15         }finally {16             i++;17             System.out.println("finally---"+i);18         }19         return i;20     }21 }

2. Console output results

      

 

3. Conclusion: The execution sequence is as follows:Try {}/catch {} finally {} return;

If there is no manufacturing exception here, it will be executed normally. The initial value of "I" is "1" and "I ++" in try-at this time, the value of "I" is "2 ", execute "I ++" in finally. At this time, the value of "I" is "3 ";

Finally, "3" is returned to the method ".

The code in finally {} is executed no matter whether an exception occurs in try {}.

  Second, return is in try catch-Key

    1. Let's look at the code section.

 1 public class Test01 { 2     public static void main(String[] args) { 3         int i = 1; 4         i=test01(i); 5         System.out.println("main--------"+i); 6     } 7      8     public static int test01(int i) { 9         try {10             i++;11             System.out.println("try-----"+i);12             return i;13         } catch (Exception e) {14             i++;15             System.out.println("catch----"+i);16             return i;17         }finally {18             i=5;19             System.out.println("finally---"+i);20         }21     }22 }

2. the console output result is

      

3. Conclusion: The execution sequence is as follows:Try {}/catch {} finally {} return;

① Finally is executed in any way (fianlly is printed), so even if there is a return statement in try, it will not be returned directly.

② After finally execution, the value of "I" should be "5". Why is the value returned to the main method "2:

I guess: the returned "I" is not the original variable "I", but the system defines "i2" for the "I value" in the try, and the returned "i2 ", however, at this time, the "I" value is still "5" defined in finally, and then "i2" is assigned to "I" in the main method ", at this time, "I" is also "2 ";

4. Note: if the return statement is added to finally, the "I" in fianlly is directly returned. At this time, the "I" output in main is "5 ".

This further explains"Code in finally will be executed no matter whether there is any exception"; After the code in finally is executed, the return statement is also executed, and the return Statement in try is no longer executed;

 

Related Article

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.