Usage of try,catch,finally in Java

Source: Internet
Author: User
Tags finally block throw exception

Java in the use of try,catch,finally, previously felt familiar, but see a Bovencai have a deeper understanding of the summary of the blog is as follows.

How to combine Java exception handling:

1.try+catch
Run the process: Run into the try block, and if an exception is thrown, go to the catch block to process it. Then execute the statement after the catch block

2.try+catch+finally

Run the process : Run into the try block, if an exception is thrown, go to the catch block, after the catch block executes, execute the code of the finally block, and then execute the code after the finally block.

If no exception is thrown, execute the try block and execute the code of the finally block. Then execute the statement after the finally block

3.try+finally
Run process: Runs into the try block, and if an exception is thrown, the program turns to the code that executes the finally block. Will the code behind the finally block be executed? No! Because you did not handle the exception, after you have encountered an exception, the method exits with an exception after you have finished executing finally.
Notice in this way that you are not catching an exception, so declare an exception after the method.

Common:
1. You can still throw a new exception in the catch block and finally block
For example throw new Exception ("illegal invoked");
If a try is not written for the newly thrown exception, declare the throw exception after the method

Conversely, if you put a layer of try,catch on the exception in the catch block or finally block, you do not have to declare the exception after the method.


Questions: There is a return statement in try {}, then the code in the finally {} immediately after this try will not be executed, when is it executed, before or after the return?

Let's look at the following code:

Public  class Test {/** * @param args Add by zxx, Dec 9, */public static void Main (string[] args) {//TODO Auto-ge Nerated method StubSystem.out.println (new test (). Test ());;} static int test () {int x = 1;try{return x;} finally{++x;}}}
---------Execution Results---------
1

public class  smallt{public static void  main (String args[]) {smallt t  = new  smallt (); int  b  =  T.get (); System.out.println (b);} public int  get () {Try{return 1;} Finally{return 2;}}}
---------Execution Results---------
2

Public  class Test {/** * @param args Add by zxx, Dec 9, */public static void Main (string[] args) {//TODO Auto-ge Nerated method StubSystem.out.println (new test (). Test ());;} int Test () {Try{return func1 ();} Finally{return Func2 ();}} int func1 () {System.out.println ("func1"); return 1;} int Func2 () {System.out.println ("Func2"); return 2;}}
-----------Execution Results-----------------
Func1
Func2
2


Analysis:

From the running results of the above 3 examples, it can be found that the function called by the return statement in the try is executed before the function called in Finally, that is, the return statement executes first, and then the finally statement executes. Return does not return the function immediately, but the return statement executes, and the returned result is placed in the function stack, and the function does not return immediately, and it does not actually begin to return until the finally statement is executed.




Usage of try,catch,finally in Java

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.