Java exception capture Try-catch-finally-return execution order-Reprint

Source: Internet
Author: User
Tags finally block

There is a return statement in the try and finally block where no exception is thrown in the 1:try block

[Java]View PlainCopy
  1. Public static int noexception () {
  2. int i=10;
  3. try{
  4. System.out.println ("I in Try block is" +i);
  5. return-I.;
  6. }catch (Exception e) {
  7. I.;
  8. System.out.println ("I in Catch-form try block is" +i);
  9. return-I.;
  10. }finally{
  11. System.out.println ("I in Finally-from try or catch block is" +i);
  12. return-I.;
  13. }
  14. }


Execution Result:

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

Execution order: Executes a try block, executes to the return statement, executes the return statement first, but does not return 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, We're not going back here. Returns the computed value in the try Block

The case does not throw an exception in the 2:try block, only the return statement in the try

Code:

[Java]View PlainCopy
  1. Public static int noexception () {
  2. int i=10;
  3. try{
  4. System.out.println ("I in try block is--" +i);
  5. return-I.;
  6. }catch (Exception e) {
  7. I.;
  8. System.out.println ("I in Catch-form try block is--" +i);
  9. return-I.;
  10. }finally{
  11. System.out.println ("I in Finally-from try or catch block is--" +i);
  12. I.;
  13. System.out.println ("I in finally block is--" +i);
  14. //return-I.;
  15. }
  16. }


Execution Result:

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 executing the return statement in try, do not return, execute finally block, after finally block execution is finished, return to the try block, return I in the try block the last value

Case 3:try The exception is thrown in the block Try,catch,finally has a return statement

Code:

[Java]View PlainCopy
  1. Public static int withexception () {
  2. int i=10;
  3. try{
  4. System.out.println ("I in try block is--" +i);
  5. i = i/0;
  6. return-I.;
  7. }catch (Exception e) {
  8. System.out.println ("I in Catch-form try block is--" +i);
  9. I.;
  10. System.out.println ("I in Catch block is--" +i);
  11. return-I.;
  12. }finally{
  13. System.out.println ("I in Finally-from try or catch block is--" +i);
  14. I.;
  15. System.out.println ("I in finally block is--" +i);
  16. return-I.;
  17. }
  18. }


Execution Result:

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

Order, after throwing an exception, execute the CATCH block, after the return of the Catch block is finished, do not return directly but do finally, because finally there is a return statement, so, execute, return the result 6

There is no return,finally in the 4,catch, Ibid., after the finally statement is executed, the value after executing the return statement in the catch is still returned, not the value modified in finally

There are exceptions in both 5:try and catch, and no return statement in finally

[Java]View PlainCopy
  1. Public static int catchexception () {
  2. int i=10;
  3. try{
  4. System.out.println ("I in try block is--" +i);
  5. i=i/0;
  6. return-I.;
  7. }catch (Exception e) {
  8. System.out.println ("I in Catch-form try block is--" +i);
  9. int j = i/0;
  10. return-I.;
  11. }finally{
  12. System.out.println ("I in Finally-from try or catch block is--" +i);
  13. I.;
  14. System.out.println ("I in finally block is--" +i);
  15. //return-I.;
  16. }
  17. }


Results:

I in try block is--10
I in Catch-form try block is--10
I in Finally-from try or catch block is--10
I in finally block is--9
Exception in thread "main" java.lang.ArithmeticException:/By zero
At exception. Exceptiontest0123.catchexception (exceptiontest0123.java:29)
At exception. Exceptiontest0123.main (EXCEPTIONTEST0123.JAVA:17)

Execution order: Exception in try block, into catch, execute to exception, execute in Finally, Judge find exception after finally execution, throw

There are exceptions in the 6:try,catch, and in finally there is a return

[JavaScript]View PlainCopy
  1. Public static int catchexception () {
  2. int i=10;
  3. try{
  4. System.out.println ("I in try block is--" +i);
  5. i=i/0;
  6. return-I.;
  7. }catch (Exception e) {
  8. System.out.println ("I in Catch-form try block is--" +i);
  9. int j = i/0;
  10. return-I.;
  11. }finally{
  12. System.out.println ("I in Finally-from try or catch block is--" +i);
  13. I.;
  14. System.out.println ("I in finally block is--" +i);
  15. return-I.;
  16. }
  17. }


Operation Result:

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

Execution order: Exception occurred in try block to Catch,catch in exception to finally,finally in execution to return statement, not check exception

There are no catch, only try and finally, the execution order is similar to the above, except that the catch block is less executed.

Java exception capture Try-catch-finally-return execution order-Reprint

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.