Java exception: try... catch... finally

Source: Internet
Author: User

Before reading the following content, I would like to ask you a question:

Will finally statements be executed?

 

Use the Integer. valueOf method to simulate an exception to illustrate the problem between try catch and finally.

 

Example 1: no exception, normal


[Java]
Public class TryCatchFinally {
 
Public static void main (String [] args ){
Try {
System. out. println ("try statement :-)");
Integer. valueOf ("2 ");
} Catch (NumberFormatException nfe ){
System. out. println ("catch statement :-)");
} Finally {
System. out. println ("finally statement :-)");
}
}
}

Example 2: no exception, normal, but add return in the try statement
[Java]
Public class TryCatchFinally {
 
Public static void main (String [] args ){
Try {
System. out. println ("try statement :-)");
Integer. valueOf ("2 ");
Return;
} Catch (NumberFormatException nfe ){
System. out. println ("catch statement :-)");
} Finally {
System. out. println ("finally statement :-)");
}
}
}

The running results of Example 1 and Example 2 are the same:

 

 

Example 3: Manufacturing exception


[Java]
Public class TryCatchFinally {
 
Public static void main (String [] args ){
Try {
System. out. println ("try statement :-)");
Integer. valueOf ("bluetooth ");
} Catch (NumberFormatException nfe ){
System. out. println ("catch statement :-)");
} Finally {
System. out. println ("finally statement :-)");
}
}
}

Example 4: create an exception. Add return to the catch statement.

[Java]
Public class TryCatchFinally {
 
Public static void main (String [] args ){
Try {
System. out. println ("try statement :-)");
Integer. valueOf ("bluetooth ");
} Catch (NumberFormatException nfe ){
System. out. println ("catch statement :-)");
Return;
} Finally {
System. out. println ("finally statement :-)");
}
}
}

The running result of Example 3 and Example 4 is the same:

 

 

From this, we can see that the finally statements are executed in the above four cases.

 

Example 5: create an exception. Add return to the catch statement.

 

Purpose: To verify whether the code behind the finally code block is executed ??????


[Java]
Public class TryCatchFinally {
 
Public static void main (String [] args ){
Try {
System. out. println ("try statement :-)");
Integer. valueOf ("bluetooth ");
} Catch (NumberFormatException nfe ){
System. out. println ("catch statement :-)");
Return;
} Finally {
System. out. println ("finally statement :-)");
}
 
System. out. println ("out of try... catch... finally statement ");
}
}

Execution result:

 

 

If you comment out the return in the catch statement, the execution result is as follows:

 

 

Example 6: create an exception. Add return to the try statement.


[Java]
Public class TryCatchFinally {
 
Public static void main (String [] args ){
Try {
System. out. println ("try statement :-)");
Integer. valueOf ("dd ");
Return;
} Catch (NumberFormatException nfe ){
System. out. println ("catch statement :-)");
} Finally {
System. out. println ("finally statement :-)");
}
 
System. out. println ("out of try... catch... finally statement ");
}
}

Execution result:

 

 

Example 7: no exception. Add return in the try statement.


[Java]
Public class TryCatchFinally {
 
Public static void main (String [] args ){
Try {
System. out. println ("try statement :-)");
Integer. valueOf ("2 ");
Return;
} Catch (NumberFormatException nfe ){
System. out. println ("catch statement :-)");
} Finally {
System. out. println ("finally statement :-)");
}
 
System. out. println ("out of try... catch... finally statement ");
}
}

Execution result (no exception, return returns, so the statement after the finally code block will not execute :-)

 

 

The above indications indicate:

 

All finally statements have been executed, but there is another situation that has not been verified. Next let's look at it.

 

Exit JVM in try... catch:

 


[Java]
Public class TryCatchFinally {
 
Public static void main (String [] args ){
Try {
System. out. println ("try statement :-)");
Integer. valueOf ("2eer ");
System. exit (0 );
} Catch (NumberFormatException nfe ){
System. out. println ("catch statement :-)");
System. exit (0 );
} Finally {
System. out. println ("finally statement :-)");
}
 
System. out. println ("out of try... catch... finally statement ");
}
}


Execution result:

 

 

If System. exit is called in a try or catch Statement (any one), the finally statement will not be executed.

 

Author: AndroidBluetooth

 

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.