Try-catch-finally-return execution order of Java exception captures __java

Source: Internet
Author: User
Tags finally block

The situation 1:try block does not throw exceptions in the try and finally blocks there are return statements [Java] view plain copy public static int noexception () {int i=10;     try{System.out.println ("I in Try blocks is" +i);    Return----I.     }catch (Exception e) {I.;     System.out.println ("I in Catch-form try blocks is" +i);    Return----I.     }finally{System.out.println ("I in finally-from try or catch blocks is" +i);    Return----I. }   }


Execution results:

I in try block Is10
I in Finally-from try or catch block Is9
The method value Is8

Order of execution: Executes the try block, executes the return statement, and then, without returning to the main method, executes a finally block, encounters a return statement in a finally block, executes-I, and returns the value to the main method. It's not going to go back here. Returns the computed value in a try block

The situation 2:try block does not throw an exception, only the try has a return statement

Code: [Java]  View plain  copy public static int noexception () {        int i=10;       try{            system.out.println ("i in try block is--" +i);            return --i;       }catch (exception e) {            --i;            system.out.println ("i in catch - form try block is--" +i);            return --i;       } finally{                       system.out.println ("I in finally - from try or catch  block is--"+i";           --i;            system.out.println ("i in finally block is--" +i);            //return --i;       }  }   


Execution results:

I in try block is--10
I in Finally-from try or catch block is--9
I in finally block is--8
The method value is--9

Order: After the statement that executes the return in a try, does not go back, executes the finally block, returns to the try block, returns the last value of I in the try block after the execution of finally block is finished

Situation 3:try The exception try,catch,finally in the block there are return statements

Code: [Java]   View plain  copy public static int withexception () {        int i=10;       try{            system.out.println ("i in try block is--" +i);            i = i/0;            return --i;       }catch (exception e) {            SYSTEM.OUT.PRINTLN ("i in catch -  form try block is--"+i";  

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.