The use of try-catch-finally finally

Source: Internet
Author: User
Tags finally block

I won't say much about the use of try-catch-finally. Search on the Internet, a lot of information.

Here I want to discuss the problem of adding a return statement to the finally block and shielding the exception .

Let's take a look at this piece of code first,


Code 1:

public class Finallytest {public static void main (string[] args) {new Finallytest (). print ();} public void print () {int i =-1; try {thread.sleep (1); i = 1/0;} catch (Exception e) {System.out.println ("at Catch blocks Step 1"); throw e;} finally {System.out.println ("at-finally block i =" + i);}}}

The code above is not compiled in eclipse because an exception is throw in the catch block, and the print method does not declare an exception to be thrown.


Now we modify the code so that it can be compiled, code 2:

public class Finallytest {public static void main (string[] args) throws Exception {new Finallytest (). print (); OID print () throws Exception {...

is to add throws Exception after the print and Main method, and then run to see the results of the operation:

At Catch blocks Step 1. At finally blocks i =-1 Exception in thread ' main ' java.lang.ArithmeticException:/by zero at Wxhx.csdn2.FinallyTest.prin T (finallytest.java:12) at Wxhx.csdn2.FinallyTest.main (Finallytest.java:5)

The program first goes to the top of the catch block and prints the at Catch blocks step 1.

But there is no immediate throw e, that is, the exception is not immediately thrown , because the exception information is in the finally block printing information before printing.

This example tells us that, finally, regardless of whether there is an exception or not, the code must be in line . (If you terminate the JVM halfway, you don't have to do it)

So when do you execute the code in the finally block?

In this example, an exception is thrown in the try block, so the code in the finally block is executed after the catch statement has been executed , before exiting the method (if throw e is executed here, the method exits).


Next look at another code, code 3:

public class Finallytest {public static void main (string[] args) {new Finallytest (). print ();} public void print () {int i =-1; try {thread.sleep (1); i = 1/0;} catch (Exception e) {System.out.println ("at Catch blocks Step 1"); throw e;} finally {System.out.println ("at finally block i =" + i); Return } } }

This code adds a return statement to the finally block compared to the previous.

Although there is a throw e statement in the catch block, the print method does not use a declaration throws Exception, or it can be compiled.

Because there is a thread.sleep (1) in the try block, the interruptedexception must be caught, but in this case, even if we remove the catch block, there is no problem , what's going on here.

Because the return statement in the finally block shields the exception.

After code 2, we already know that the exception will execute the code in the catch block before the finally block is executed, but it will not exit the method, and it will move to the finally block before exiting the method, and there is a return statement in the finally block, so the method normally exits , the exception generated in the try block will not have an opportunity to be thrown.

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.