Reputation: 3247
I am new to Java language and cannot understand the behavior of finally block in this program. This program should exit after printing BC whereas it is printing BCD. Please help.
class Main
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod() throws Exception
{
throw new Exception(); /* Line 22 */
}
}
Upvotes: 2
Views: 263
Reputation: 2975
Your program is functioning in the following way: 1. Call a bad method that throws an exception 2. Catch the exception 3. Execute the finally block 4. Resume
In Java, when an exception gets thrown it does not necessarily end program execution if the exception is handled. Your program does not actually handle the exception but catches it anyway and this is enough for the JVM to think it's ok to resume execution.
The output BCD is quite the right output.
Upvotes: 1
Reputation: 8169
The finally
block doesn't finalize the program, it just ensure to execute every time the try catch block runs whereas there is an exception or not..
Upvotes: 4
Reputation: 12538
Think of the finally block as an independent block of code(s) you'll still expect your method to continue executing irrespective of whether an exception occurs or not.
And so in your case, badMethod
throws an exception which is consequently caught by the catch block, your main then continue by executing the finally block independently.
In other words, if badMethod
decides not to throw an exception, your finally block would still continue execute before reaching the end of the method.
Therefore with finally been an independent block it is then possible to also do something like this in your main code if prefered.
try
{
fooA();
}
finally
{
fooB();
}
Upvotes: 1
Reputation: 11
Finally is the must executable block of java program. It allows all the allocated resources of the currently running program to get free and make it available for the other applications if required.
This is mainly used when we share the common resources like Database or the devices.
Upvotes: 1
Reputation: 8764
The finally
block is processed after the try
or catch
block runs (depending on whether an exception was thrown/caught or not). If the exceptions were all caught properly and handles by the catch
, it will run finally
and then continue running the rest of the method.
Upvotes: 1
Reputation: 14738
Which is correct. The above code will.
badMethod
and will failcatch
finally
Upvotes: 1
Reputation: 7011
Finally is called at the end of a try/catch block. It gets called even if the try fails and the catch is executed. The Finally
block itself is only not executed if the program is killed somehow (JVM dies, forced to close, etc.)
In your example D is executing because it is outside of the try/catch/finally{} blocks.
There is a nice blog post on catching exceptions and the try/catch/finally behaviour here.
Upvotes: 1
Reputation: 887453
There is nothing to cause the program to exit.
The whole point of a catch
block is to catch the exception and prevent it from propagating further.
After catching an exception, execution continues.
Upvotes: 4
Reputation: 1500535
You're catching the exception (in the catch block) and not rethrowing it - so you're effectively handling the exception, and execution then proceeds as if it weren't thrown. The program only exits because it reaches the end of the main
method - it's not like it's terminated abruptly.
If you change your code to either rethrow the exception from the catch block or just don't catch it in the first place (both of which will require you to declare that main
throws Exception
, of course) then it won't print D
.
Upvotes: 8
Reputation: 62593
It would print BC
if you re-throw the exception in catch
block.
catch (Exception ex)
{
System.out.print("B");
throw ex;
}
Then you'll have to declare your main as
public static void main(String [] args) throws Exception
Upvotes: 1