Try catch finaly in Java sequential invocation order

Source: Internet
Author: User
Tags try catch

Self-summary, what is insufficient or good plan, hope that we give correct, grateful!

Purpose: Try catch finaly in the order of execution, and everyone review it again.

Method: Debug to confirm the order of execution. (need to introduce junit packages)

Needless to say, Debug is the only standard for verifying the Order of code execution, haha ...

Test One:

New test class: Exceptiontest

 Packagecom.core.test007;Importorg.junit.Test; Public classexceptiontest {@Test Public voidMain () {put (test007 ()); }     Public inttest007 () {intA = 10; Put ("Normal"); Try{put ("Try"); //A = 1/0;Put ("Try return before"); returnA; }Catch(Exception e) {put ("Catch"); } finally{put ("Finally"); } put ("Method return before"); return0; }         Public Static voidput (Object obj) {System.out.println (obj); }}

The printed information is

Normal Try Try return before finally10

Debug, execute the statement order is: 1) The statement of the try body, print "Try"

2) in the try body, print "Try return before"

3) Jump into finaly body, print "finaly"

4) Execute the return statement in the try body again

5) Jump directly out of the function body and return to the main function body, print 10

Test Two:

Put the above Exceptiontest class code in [int a = 1/0;] Before the comment is deleted, save the run again to print the information:

Normal Try Catch finally  return  before0

Debug, execute the statement order is: 1) The statement of the try body, print "Try"

2) EXECUTE statement [int a = 1/0;] , enter the catch body and print "catch"

3) Jump into finaly body, print "finaly"

5) Jump out of the finaly body, print "method return before"

6) Execute return 0

7) Return main function, print 0

Summary: 1. The finaly is executed under any circumstances.

2. When executing a statement in the try body that has an exception (the exception statement in this case is: [int a = 1/0;]) , all statements after the exception statement in the try body are no longer executed during this execution, but instead enter the catch statement.

Try catch finaly in Java sequential invocation order

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.