Lets begin this post by asking the output of the following program.
This question is asked a lot in interviews . Even I was asked twice the same question in
two different interviews.
Why interviewers asks this question - they want to understands that whether the candidate really understands the flow of try-catch-finally block or not.
As in most of the case candidate knows that finally block executes in all case. But she/he starts getting confused when interviewer combine it with return statements.
Lets first have a look on the oracle's exception tutorial. Here is what oracle says about the finally block
The
So now it is clear that whatever happens in the try or catch blocks finally will must be executing.
And here is the output of the code I started with.
in try block
in catch block
in finally block
It's number 3
Lets make a bit modification in the above code so it can look like this -
We have made a small change and used the System.exit(0) in the catch block.
Here is specific cases from the oracle docs which states when a finally block not executed.
Note: If the JVM exits while the
And hence here is the output of the above program -
in try block
in catch block
Thanks for reading this post:)
public class Main { public static void main(String[] args) { System.out.println("It's number " + new Main().complexBehaviour()); } private int complexBehaviour() { try { System.out.println("in try block"); return new Integer("o1"); } catch (Exception e) { System.out.println("in catch block"); return new Integer("02"); } finally { System.out.println("in finally block"); return new Integer("03"); } } }
This question is asked a lot in interviews . Even I was asked twice the same question in
two different interviews.
Why interviewers asks this question - they want to understands that whether the candidate really understands the flow of try-catch-finally block or not.
As in most of the case candidate knows that finally block executes in all case. But she/he starts getting confused when interviewer combine it with return statements.
Lets first have a look on the oracle's exception tutorial. Here is what oracle says about the finally block
The
finally
block always executes when the try
block exits. This ensures that the finally
block is executed even if an unexpected exception occurs. But finally
is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return
, continue
, or break
. Putting cleanup code in a finally
block is always a good practice, even when no exceptions are anticipated.So now it is clear that whatever happens in the try or catch blocks finally will must be executing.
And here is the output of the code I started with.
in try block
in catch block
in finally block
It's number 3
Lets make a bit modification in the above code so it can look like this -
public class Main { public static void main(String[] args) { System.out.println("It's number " + new Main().complexBehaviour()); } private int complexBehaviour() { try { System.out.println("in try block"); return new Integer("o1"); } catch (Exception e) { System.out.println("in catch block"); System.exit(0);//Terminates the process } finally { System.out.println("in finally block"); return new Integer("03"); } } }
We have made a small change and used the System.exit(0) in the catch block.
Here is specific cases from the oracle docs which states when a finally block not executed.
Note: If the JVM exits while the
try
or catch
code is being executed, then the finally
block may not execute. Likewise, if the thread executing the try
or catch
code is interrupted or killed, the finally
block may not execute even though the application as a whole continues.And hence here is the output of the above program -
in try block
in catch block
Thanks for reading this post:)
Great explanation...
ReplyDelete