kaustubh sinha
kaustubh sinha

Reputation: 79

Exception handling in java using finally

public class CheckProg {
    public int math(int i) {
        try {
            int result = i / 0;
       // throw new IOException("in here");
        } catch (Exception e) {
            return 10;
        } finally {
            return 11;
        }
    }
    public static void main(String[] args) {
        CheckProg c1 = new CheckProg();
        int res = c1.math(10);
        System.out.println("Output :" + res);
   }

Question: If I run the above code, I get the result as Output: 11

Why? Shouldn't the exception be caught before in the catch block and returned?

Upvotes: 5

Views: 102

Answers (5)

Christeena Vincent
Christeena Vincent

Reputation: 1

The "finally" keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.

try {

final int x=100;
System.out.println("The value of x is"+ x);

}

catch(Exception e)
{
    System.out.println(e);  
}
finally
{
    System.out.println("Finally Block executed");
}

Upvotes: 0

Raúl
Raúl

Reputation: 1552

It is pretty much self-explanatory. Catch block is executing because there is an exception. Finally will execute as its execution is guaranteed irrespective of an exception.

Note - If you have given some other statement(s) instead of return, both statements have been executed. but in case of return, only last return is being executed.

} catch (Exception e) {
                System.out.println("10");
            } finally {
                System.out.println("11");
            }

Upvotes: 0

vysh
vysh

Reputation: 166

Finally block gets executed whether or not an exception occurs.

Upvotes: 0

Stefaan Neyts
Stefaan Neyts

Reputation: 2067

Yes. But the finally block is always executed afterwards and overrules the return value!

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503409

Shouldn't the exception be caught before in the catch block and returned ?

It is being caught, and that return statement is being executed... but then the return value is being effectively replaced by the return statement in the finally block, which will execute whether or not there was an exception.

See JLS 14.20.2 for details of all the possibilities. In your case, this is the path:

If execution of the try block completes abruptly because of a throw of a value V, then there is a choice:

  • If the run-time type of V is assignment compatible with a catchable exception class of any catch clause of the try statement, then the first (leftmost) such catch clause is selected. The value V is assigned to the parameter of the selected catch clause, and the Block of that catch clause is executed. Then there is a choice:

    • If the catch block completes normally [ ... ignored, because it doesn't ]

    • If the catch block completes abruptly for reason R, then the finally block is executed. Then there is a choice:

      • If the finally block completes normally [ ... ignored, because it doesn't ]

      • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded).

So that bottom line is the important one - the "reason S" (in our case, returning the value 11) ends up being the way that the try statement completes abruptly.

Upvotes: 7

Related Questions