In readFileMethod1, an IOException is explicitly catched before throwing it at the method level to ensure that the finally block is executed. However, is it neccessary to catch the exception? If I remove the catch block, shown in readFileMethod2, does the finally block get executed as well?
private void readFileMethod1() throws IOException {
try {
// do some IO stuff
} catch (IOException ex) {
throw ex;
} finally {
// release resources
}
}
private void readFileMethod2() throws IOException {
try {
// do some IO stuff
} finally {
// release resources
}
}
The finally still gets executed, regardless of whether you catch the IOException. If all your catch block does is rethrow, then it is not necessary here.
No, it's completely unnecessary to catch an exception if you're not going to do anything other than throw it.
And yes, the finally block will still be executed.
No, it isn't necessary to catch the exception unless you can't rethrow it in your method. In the code you posted the readFileMethod2 is the correct option to follow.
the finally always get executed in try catch context ... for more info check http://download.oracle.com/javase/tutorial/essential/exceptions/finally.html
finally is executed always irrespective of whether an exception is thrown or not. Only if the JVM is shut down while executing the try block or catch block, then the finally clause will not be executed. 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.
Related
let's consider the following code:
try {
throw new Exception("from try")
} catch (Exception e) {
throw new Exception("from catch")
} finally {
throw new Exception("from finally")
}
It gives:
Exception thrown
java.lang.Exception: from finally
<...>
So it looks that finally is executed before catch and terminates execution flow.
What could I do if want to see both exceptions?
So it looks that finally is executed before catch and terminates
execution flow.
That is not correct. finally is executed after a correspondeing catch, not before. The issue is that your catch block executes, and then after that the finally block is guaranteed to execute.
This post will help you I guess:
https://stackoverflow.com/questions/3779285/exception-thrown-in-catch-and-finally-clause
My code is below :-
class Varr {
public static void main(String[] args) {
try {
System.out.println(10/0);
}
catch(ArithmeticException e) {
System.out.println("catch1");
System.out.println("catch1");
throw new ArithmeticException ("Exce");
}
finally {
System.out.println("finally");
}
}
}
Output is :-
catch1
catch1
finally
Exception in thread "main" java.lang.ArithmeticException: Exce
at one.Varr.main(Varr.java:22)
As per my knowledge the flow has to be first try then catch and finally at last but as per the output the flow is try then few lines of catch upto the throw exception statement and then finally and the throw exception statement of catch block at last.
Why is there discrepancy in flow, I mean why finally was executed before the throw new exception statement of catch block
Because a block of finally, by definition, has to be executed no matter the outcome of the try or catch clauses.
In your case, the run-time knows there is an exception that has to be propagated upwards, but before doing so, it executes whatever is inside the finally block.
When the finally block is finished, it propagates any exception that might have been raised, or the flow continues otherwise.
You can take a look at the essentials of finally
The finally block always executes when the try block exits. This
ensures that the finally block is executed even if an unexpected
exception occurs.
I've tried to throw the same excpetion in a finally block, while the previously throwed expcetion was not catched. I expected that we have two object of Excpetion type that shal be thrown. Since we need in two catch clauses as the following:
public static void main(String[] args) {
try {
try {
try {
throw new Exception();
} finally {
System.out.println("finally");
throw new Exception();
}
} catch (Exception ex) {
System.out.println("catch");
} finally {
System.out.println("finally");
}
} catch (Exception ex) {
System.out.println("catch");
}
System.out.println("finish");
}
But that program prints:
finally
catch
finally
finish
That is, the second catch clause was not entered. Why?
When you throw an exception in the finally block, the first exception silently disappears.
It's in the JLS Chapter 14.20.2
If the finally block completes abruptly for reason S, then the try
statement completes abruptly for reason S.
This is true how ever you entered the finally block. If you entered it by throwing an exception T that exception can not be catched anymore.
When you throw an exception from the finally block, it supresses any exception thrown from the try block, therefore there was only one exception to catch.
The first catch cluase already caught that exception, so the second one had nothing to catch.
Your first try-catch is already trying to catch it, the extra try blocks aren't there for any particular reason. If you try to throw more exceptions you'll notice that you'll get a syntax error, Unreachable code
Basically, keep it in one try block
I am trying to put a while loop inside a try /catch block. To my curiosity finally of that try catch is not executed when the while loop exits. Can some explain what is happening actually?
I tried to google, but cnould not find any details.
I assume your code looks like this:
try
{
while (...)
{
// ...
}
}
catch (FooException ex)
{
// This only executes if a FooException is thrown.
}
finally
{
// This executes whether or not there is an exception.
}
The catch block is only executed if there is an exception. The finally block usually executes whether or not an exception was thrown. So you will probably find that your finally block is actually being executed. You can prove this to yourself by placing a line there that causes some output to the console.
However there are situations in which the finally block doesn't run. See here for more details:
Does a finally block always run?
It can happen only if your program exits either by using System.exit() or if an Error or Throwable were thrown (vs. Exception that will be caught).
Try the following:
public static void main(String[] args) {
try{
System.out.println("START!");
int i=0;
while(true){
i++;
if(i > 10){
System.exit(1);
}
}
}
catch (Exception e) {
// TODO: handle exception
}
finally{
System.out.println("this will not be printed!");
}
}
Considering the below code, does finally still run if there was an exception thrown in the catch clause?
try {
//code here throws exception
}
catch(Exception ex) {
//code catches above exception however code here also throws another exception
}
finally {
//does this code even run considering the exception thrown in the above catch clause??
}
Yes it does. It runs regardless of what happens in the try/catch (assuming the JVM doesn't shut down for some reason)