Java program execution details

Source: Internet
Author: User
Tags finally block

Java program execution details

Note !!! I recorded not necessarily correct. Please do it yourself and get your answer. After all, I am still a novice.

 

The following is an example of how the program is executed after an exception occurs, that is, try... catch... affects the execution sequence of the program when the exception occurs:

1. Check the sequence of abnormal program execution without using try... catch ....

public class TException {public static void main(String[] args) {int i = 1 / 0;System.out.println("after exception");}}

  

Running output:

Exception in thread "main" java.lang.ArithmeticException: / by zeroat exception.TException.main(TException.java:5)

As you can see, when the program encounters an exception and does not handle the exception, the program will be interrupted in the case of the exception, and the statements following the exception will not be executed;

2. Check the effect after try... finally... is added.

public class TException {public static void main(String[] args) {try {int i = 1 /0;}finally{System.out.println("final block");}System.out.println("after exception");}}

Running output:

final blockException in thread "main" java.lang.ArithmeticException: / by zeroat exception.TException.main(TException.java:6)

We can see that the statements in the finally block are executed, but the statements outside the block are not executed.

3. Check again and add try... catch ....

public class TException {public static void main(String[] args) {try {int i = 1 / 0;} catch (Exception e) {System.out.println(e);}System.out.println("after exception");}}

Running output:

java.lang.ArithmeticException: / by zeroafter exception

We can see that the statements in the catch Block are executed, and the statements outside the block are also executed.

4. Add try... catch... finally... to the effect of exception handling in catch.

public class TException {public static void main(String[] args) {try {int i = 1 / 0;} catch (Exception e) {System.out.println(e);}finally{System.out.println("finally");}System.out.println("after exception");}}

Running output:

java.lang.ArithmeticException: / by zerofinallyafter exception

In this case, the catch and finally blocks and other statements are executed.

5 .. add try... catch... finally .. however, catch does not handle exceptions, but throws them up (whether the original exception is thrown or encapsulated as a runtime exception ).

public class TException {public static void main(String[] args) {try {int i =1/0;} catch (Exception e) {throw e;}System.out.println("after");}}

Running output:

Exception in thread "main" java.lang.ArithmeticException: / by zeroat exception.TException.main(TException.java:6)

At this time, even if the catch clause is added, the statement after the block is still not executed.

Conclusion:

1) if the exception is not processed, the program will be interrupted when the exception occurs, and subsequent statements will not be executed.

2) finally: The finally content will be executed no matter whether the program to be wrapped by try has encountered any exceptions or whether it has been processed after the exception occurs.

3) catch: catch occurs. In addition, the exception is handled in catch, that is, the VM is notified. This exception has occurred and is expected by me, in addition, I have also handled this exception. You can continue to execute the exception. At this time, the program will continue to execute the exception. If catch occurs, but the exception is not handled in it, instead, throw the exception to the upper layer for processing. At this time, the program will still be interrupted.

Note that try cannot exist independently. It must exist together with one or both of catch or finally. Otherwise, a compilation error is reported and cannot be compiled.

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.