return value when Java exception

Source: Internet
Author: User
Tags finally block

First look at a small program:

Public class javaexception{         public static  void main (String[] args)          {                     SYSTEM.OUT.PRINTLN (Test ());         }          public static int test ()           {                    int i=0;                    try                    {                             i = 1/0;//throws non-inspected exception                               i=100;                              system.out.println ("in try  throws an exception after it is not executed here! ");                    }                    catch (exception e)                     {                             i=1;                              System.out.println ("in catch i=" +i);                              return i;     //Comment 1                    }                    finally                    {                              i=2;                             system.out.println ("in finally i=" +i);                    }                    i= 3;                    return i;         }}

The first thing to think about is when we comment out the return statement in Note 1, which is the catch , and what is the final result of the output of the program without commenting it out?

If you do not comment out the return in the catch , the return result is 1, and The return statement in the comment- out catch Returns the result is 3 .

for what? Let's debug the program, and in the place where the exception is thrown is the 1/0 line breakpoint, and then debug.

I use eclipse, so direct shortcut key F6(skip function, function as a whole, do not enter function), jump directly to catch, so it is 1/0 There's something wrong here.

continue to F6 until the return statement in the catch .

continue stepping, found to skip directly over the return statement, continue to execute the finally statement block, continue to F6 stepping , found to execute finally The block executes the return in the catch block directly .

Now let 's comment out the return statement in the catch block and re-debug it to see how the program executes.

The program also throws an exception where it 1/0 , executes the statement in the catch block, and then executes the statement in the finally block. However , when no return statement is in the catch block, the statement after the finally block is executed until the return statement is encountered .

so when there is a return in the catch ,the test () return value is 1and there is no return statement in the catch block. The return value of test () is 3.

so the question is, why is the return value of test () 1 instead of 2 when there is a return in the catch? Finally , I was assigned a value of 2 , why did not return 2.

before answering this question, let's take a look at two ways of exiting in java.

1. encountered a returned instruction (return statement)

2. an exception was encountered and no value was returned to the call without a search to the exception handler.

So when there is a return in the catch, the program is flagged when it encounters a return, and the finally block is only performing the final cleanup work, such as the release of the database connection. However, the return statement will not be modified .

Note: Do not add a return statement to the finally block , and if you add a return at Finally, you will change your return statement. If you use an IDE like Eclipse , a return in finally will alert you that the code is not working properly.

The function of debugging shortcut keys commonly used in Ps:eclipse

F5 (Step Into): just one step into the method body that is entered when the code encounters a calling method

F6 (Step Over): One-step skip, that is, when this step is a method as a whole execution, not into the method

F7 (Step return): Jump out, just jump out of the currently executing method body, return to the call

F8 (Resume): Start again, when execution to a breakpoint, the program is suspended, F8 is to let the program continue to execute until the next breakpoint is suspended


return value when Java exception

Related Article

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.