Execution sequence of the program when a Java exception occurs

Source: Internet
Author: User

Basic knowledge:

1. The try code segment contains code that may generate exceptions;
2. A try code segment is followed by one or more code segments;
3. Each catch code segment declares a specific exception that it can handle and provides a solution;
4. When an exception occurs, the program terminates the current process and executes the catch code segment based on the obtained exception type. If there are multiple catch codes that meet the condition, only the first one is executed;
5. Code in the finally segment will be executed no matter whether an exception occurs or not.
6. In a try statement block, the capture statements of the basic exceptions cannot be written on the top of the exception capture statements of the subclass.

 

Let's look at an example:

 /** <Br/> * @ author lansine <br/> */<br/> public class T1 {</P> <p> /** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> string S = "1"; <br/> try {<br/> S = "2"; <br/> system. out. println (s); <br/> If (S = "2") <br/> throw new exception ("H "); <br/>} catch (exception e) {<br/> S = "3"; <br/> system. out. println (s); <br/>}finally {<br/> S = "4"; <br/> system. out. println (s); <br/>}< br/> S = "5"; <br/> system. out. println (s); </P> <p >}</P> <p>}

The output result is 2, 3, 4, and 5 (the comma here is only used for display ). The preceding statement is very clear, but adding return to the above structure becomes a little complicated, as shown in

 /** <Br/> * @ author lansine <br/> */<br/> public class T2 {</P> <p> /** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> string S = "1"; <br/> try {<br/> S = "2"; <br/> system. out. println (s); <br/> return; <br/>}catch (exception e) {<br/> S = "3"; <br/> system. out. println (s); <br/>}finally {<br/> S = "4"; <br/> system. out. println (s); <br/>}< br/> S = "5"; <br/> system. out. println (s); </P> <p >}</P> <p>}

The output result is 2, 4, that is, in the try structure, although the return statement is used to force the function to return the result, it is not executed, but the finally implementation is still executed. However, other statements except finally are not executed.

A more popular example is:

Import Java. io. *; </P> <p>/** <br/> * @ author lansine <br/> */</P> <p> public class mine {<br/> Public static void main (string argv []) {<br/> mine M = new mine (); <br/> try {<br/> system. out. println (M. amethod (); <br/>} catch (exception e) {<br/> // todo automatically generates catch blocks <br/> // E. printstacktrace (); <br/> system. out. println ("I know"); <br/>}< br/> system. out. println ("finished"); <br/>}</P> <p> Public int AM Ethod () throws exception {<br/> try {<br/> fileinputstream Dis = new fileinputstream ("hello.txt"); // 1, throwing an exception <br/> system. out. println ("after an exception"); <br/>} catch (exception ex) {<br/> system. out. println ("No such file found"); // 2. catch catch exceptions and execute <br/> // throw new exception ("handle above"); <br/> return-1; // 4, return <br/>}finally {<br/> system. out. println ("doing finally"); // 3. finally will be executed before return. <Br/>}</P> <p> system. out. println ("after Code"); </P> <p> return 0; <br/>}< br/>}

The result is:

No such file found <br/> doing finally <br/>-1 <br/> finished <br/>

If an exception is thrown in the Catch Block, the result is:

No such file found <br/> doing finally <br/> I understand <br/> finished

 

Note: If the exception is thrown up until the main function has not been caught, the program will be terminated by the exception.

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.