简体   繁体   中英

How does a return statement work after a finally block in java?

Here's an example from the book "Java All-in-one desk reference"

public class CrazyWithZeros {
public static void main(String[] args) {
    try {
        int answer = divideTheseNumbers(5, 0);
    } catch (Exception e) {
        System.out.println("Tried twice, still didn't work!");
    }
}

public static int divideTheseNumbers(int a, int b) throws Exception {

    int c;
    try {
        c = a / b;
        System.out.println("It worked!");
    } catch (Exception e) {
        System.out.println("Didn't work the first time.");
        c = a / b;
        System.out.println("It worked the second time!");
    } finally {
        System.out.println("Better clean up my mess.");
    }
    System.out.println("It worked after all.");
    return c;

}

}

After the finally clause executes, the ArithmeticException is thrown back up to the calling method. The statement System.out.println("It worked after all."); would never be executed in this case. But what happened to the return c; ?

I wonder whether the return statement would still return the result of the division or not?

========

I tried to replace " System.out.println("Better clean up my mess."); " with " System.out.println(c); ", then it's compiled and the results are as follows:

 Didn't work the first time. 0 Tried twice, still didn't work! 

I can't believe the variable c could be calculated. (it's the wrong number, though) Why could this happen?

Then I also tried to replace " System.out.println("Better clean up my mess."); " with " return c; " and deleted the statements below the finally block, it's compiled again...Since the finally block is executed whether or not any exceptions are thrown by the try block or caught by any catch blocks, the return c; should be executed. But here're the results:

Didn't work the first time.

looks like c couldn't get returned...

return c is not executed either. It goes straight to the catch block in your main method.

What do you expect performing an error-prone operation the second time? :)

It is gonna generate an exception of the same type you came in the catch block with, but at that time it would not be handled - you don't have another try-catch within this catch block.

The finally is executed always regardless either an exception occurs or a normal process flow proceeds. In your case, you come to the finally block with the exception and throw it to the caller ( main ) where it gets handled by its own catch block.

I wonder whether the return statement would still return the result of the division or not?

What do you want to return? You haven't initialized the variable c and there is no correct record to this variable. Therefore, Java doesn't allow to write "something unexpected or unpredictable" into the c .

  1. A method returns some value if it is executed without exception or error.
  2. finally block does not have any impact on whether return statement will be executed or not. (of course, finally block should not throw any further exception)
  3. catch block determines whether that exception should be propagated further or handled within the method.

In the given example, an ArithmeticException is thrown from try block and it will be handled by respective catch block. As catch blocks again throws the same exception, given return statement would never execute.

In short, return c; is never executed in above program and variable c will be deleted as any other local variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM