Trouble in understanding the working of try-catch-finally blocks - java

Please correct me if I'm wrong, but, I believe that the statements in the try block get executed first, then, if any exception occurs, the statements in the finally block get executed and then the statements in the catch block get executed. If no exception occurs, then the statements in the finally block are executed once the statements in the try block are executed and the statements in the catch block are skipped.
If my above conception is not mistaken, then I don't understand why this piece of code doesn't work:
// sock is an object of the class Socket
public void run() {
try {
in = sock.getInputStream();
scan = new Scanner(in);
out = sock.getOutputStream();
pw = new PrintWriter(out);
} catch (IOException e) {
e.printStackTrace();
} finally {
sock.close();
}
}
It still says that I need to surround the statement in the finally block with try-catch.

No! the statements in the try block are executed first. Then if any exceptions occurs then the catch block statements are executed. And finally block is executed lastly. finally block gets executed even if an exception occurs in the try block. In other words if no exception occurs then first the try block is executed and then the finally block is executed.

In general If your catch block can catch the Exception thrown by the try block.
try -> finally (If No Exception)
try -> catch -> finally (If Exception)

The statements within the catch block are executed whenever an exception is encountered (which can be caught by the listed catch blocks). The statements within the finally block will always get executed, whether or not an exception was thrown within the try block.
Your sock.close(); statement could potentially throw an IOException, so either you should have another try-catch block wrapping the inner try-catch or add a throws declaration to your run() method (and the caller of the method can have a try-catch block surrounding the call).
EDIT (as per comment):
Statements within catch get executed first, then the statements within finally.

Related

Java 'finally' clause in a nested 'try' block

Will the following finally clause be executed, if an exception is thrown by the PrintWriter?
try{
PrintWriter out = new PrintWriter(filename);
try {
//output
} finally {
out.close();
}
} catch {
//handle exception
}
If the PrintWriter throws an exception, then the nested try block will never get executed, but why the nested finally clause will still be executed, even it's nested and skipped?
Updates:
I ran some tests, if an exception is thrown before the nested try clause, that nested finally will not be executed.
If the exception is thrown inside the nested try clause, then the inner finally and the outter catch will be executed.
No because the inner try block will not be reached when an exception occurs before and therefore the finally block is not reached either.
Finally block is always executed whether exception is handled or not. Even though their is an error and it reaches to catch block, it will go to finally block to execute the piece of code.
finally block is a block that is used to execute important code such
as closing connection, stream etc.
So, Inside try{} block you placed try and finally, but you asked about the catch of outside try ,thus its not going inside the first try block.That finally wont work.
P.S. : If you put finally something like this:
try{
try{...}
finally{...}
}catch(Exception e){...}
finally{... }
//in case of exception also , the outside finally is going to work.
P.S.: Though you got your answer , but the concept is for reference of other naive programmers
An uglier variant (sometimes generated by the IDE) one sees also:
// *** UGLY
PrintWriter out = null;
try {
out = new PrintWriter(filename);
//output
} catch (IOException e) {
//handle exception
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e2) {
// IGNORE
}
}
}
That explains the code a bit: as close may throw an IOException too, the code becomes cumbersome. Your code still needs nested exceptions.
With try-with-resources this can & should be written as:
try (PrintWriter out = new PrintWriter(filename)) {
//output
} catch (IOException e) {
//handle exception
} // Automatically closes.
And no longer nested exceptions.
The biggest advantage is that you need not catch any exception, and just add a throws IOException in the method signature.

java try catch when is the program flow interrupted?

Hello i am not that familiar with Exception Handling in Java so:
As the Topic says in a basic try/catch block, when i catch an Exception in the Try block, when is the Program flow interrupted?
try{
//some code that raises an Exception
}catch(Exception e){
// react to interrupt or continue program flow
}finally{
// always done after the catch
}
//when is this code executed?
The finally statement is always executed after a try catch, so what has to be done in the catch part to either interrupt the program or let the program continue?
Instruction flow is interrupted when any exception occurs anywhere.
If the exception occurs in a try range, and the exception class is "assignable to" the class specified in the catch clause, then control is transferred to the beginning of the catch block. (If the exception class is not "assignable" then the exception simply "bubbles up" to the next outer try block or the thread EH.)
If the catch block does not throw an exception (or rethrow the original one) then the exception is considered "handled", and execution continues immediately after the list of catch clauses for that try. This may mean that flow goes into the finally block before continuing at the end of the finally.
If the exception is not caught, or an exception is (re)signalled in the catch block, then any following finally block is executed, before the exception "bubbles up".
If a return is executed in the try or catch block then any finally is executed before the return takes effect.
If you catch the exception in the catch block, and the finally block doesn't throw any exception, the statements after the finally block will be executed immediately after the finally block is executed.
If you don't catch the exception in the catch block, or you throw a new exception in either the catch block or the finally block, the statements after the finally block will not be executed, and the next code that will be executed is the most immediate enclosing catch block that can handle the thrown exception.
Catch() block is to notify/handle the exceptions.
Quoting from docs,
The catch block contains code that is executed if and when the
exception handler is invoked. The runtime system invokes the exception
handler when the handler is the first one in the call stack whose
ExceptionType matches the type of the exception thrown. The system
considers it a match if the thrown object can legally be assigned to
the exception handler's argument.
and
As the Topic says in a basic
try/catch block, when i catch an Exception in the Try block, when is the Program flow
interrupted?
Catch blocks are meant to handle the exceptions. So if there are no errors Catch block will not be executed, however finally() will be executed
try {
System.out.println("Throw exception");
int i = 5/0;
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
e.printStackTrace();
}finally{
System.out.println("Doubt solved if line prints");
}
System.out.println("After exception");
here the code try it by your self i think there will not be need of explanation after execution of this till tile exception occur it will directly go to the catch and even then it will go to the execution of finally and then further code.
Execute following code to understand flow of exception for exception.
try {
//line 1
//line 2
throw new Exception(); //line 3
// line 4
// line 5
} catch (Exception e) {
// if exception occur at line 3, line 4 & 5 will not executed because of interrupt in execution flow.
// since your program is interrupted when exception occur.
System.out.println("Catch block will execute after exception !");
}finally{
System.out.println("Finally block will execute after exception !");
}
// your program will continue its execution.
System.out.println("Code after the finally block also exectute after the exception !");
Output
Catch block will execute after exception !
Finally block will execute after exception !
Code after the finally block also exectute after the exception !

Safe try/catch java

try {
bufferedReader = new BufferedReader(new FileReader(new File(file,FILENAME)));
String readLine = bufferedReader.readLine();
//do stuff
} catch(Exception e) {
throw e;
} finally {
if (bufferedReader!=null)
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
will the bufferedReader closing be invoked in any case in this code?
yes it invokes in any case( if it is not null in you case).According to java docs
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.
If you are using Java7 then I strongly recommond to use try-with-resources Statement.Then you do not require to write the finally block in your code.
try-with-resouce example
try (BufferedReader bufferedReader =
new BufferedReader(new FileReader(new File(file,FILENAME)));) {
String readLine = bufferedReader.readLine();
//do stuff
} catch(Exception e) {
throw e;
}
Note:
finally block won't execute in only one case. That is when JVM shutdown(generally with System.exit() statement or when the JVM process is killed externally). In all other cases the finally is guaranteed to execute
Finally is always executed, even if the exception is thrown or not. So it will execute the bufferedReader.close(); when bufferedReader is not null
I would refer you to this question:
In Java, is the "finally" block guaranteed to be called (in the main method)?
Basically, the finally block will always get run, with one exception: If the JVM exits before the finally block executes.
As to whether the bufferedReader executing - as written, so long as it's not null, yes, with the above noted exception
Yes, this code will close bufferedReader in any situation. But in general the code looks like a mess. Java 7 try-with-resources is the best solution of closing resources
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(file,FILENAME))) {
//do stuff
}
finally in a try/catch block means that it will happen no matter what.
Also, the exception would be better stated as IOException.
There is really no reason to have a catch block throw an exception in this situation.
Note: It is true that invoking System.exit() will terminate your could as soon as it is encountered and will result in the application being terminated by the JVM (Java Virtual Machine).

Is it necessary to put catch statements after a try-block?

I just want to know is it necessary to put catch after try block, or can we use try blocks without a catch block?
You need to put either catch or finally block after try.
try {
}
finally {
}
or
try {
}
catch (Exception e) {
}
is it necessary to put catch after try block ?
Nope, not at all. Its not mandatory to put catch after try block, unless and until the try block is followed by a finally block. Just remember one thing, after try, a catch or a finally or both can work.
we can use try without catch block?
Yes, you can. But that will be a bad practise. Since, you are writing a try block, you should be writing catch block ( for catching the exception) and a good practise to follow it by a finally block.
Yes you can... but you must put a finally block after try. So you can do it like this:
try
{
}
finally
{
}
or
try
{
}
catch(Exception e)
{
}
Yes you can write try without catch. In that case you require finally block. Try requires either catch or finally or both that is at least one catch or finally is compulsory.
try{
// throw exception
} finally{
// do something.
}
But you should avoid this case cause in this case you will loose exception details. So if you don't want to handle it in here then simply throw that exception.
try without a catch block is a syntax error because it makes no sense (unless you also want to use a finally block). The only reason to use try is in order to catch the exception (or do a finally) from within that block
In Java 7 the try-with-resource statement doesn't need catch or finally clause
try(InputStream is = new FileInputStream(..))
{
is.read();
}
Yes you can use finally instead but to be more practical I use "throws Exception" function if I can because using try and catch blocks makes code harder to read.
First thing to remember is that you have to know what the purpose of the try-catch-finally block is.
The try block is used to test the code written inside it. If the code causes an exception, it throws the exception to the catch block.
The catch block is used to handle the thrown exception like, assume that you wrote a code that prompt the user to insert numbers only. But the user inputted a letter, thus the code throw an exception. The exception then would be caught by the catch block. Then the catch block prompt the user to re-input the data. This is what you call exception handling. But if you want to just leave the catch block empty is fine.
You may write try without the catch keyword following it but, you have to write the finally after the try block.
The code in the finally block will always be executed no matter what. You usually write codes in the finally block to close resources opened in the try block like files or database connection.
You can use the try-with-resources in place of the finally block (available in java 8).
So, you can write try followed by catch then followed by finally like the following example :
try{
//code
}
catch(Exception ex){
//code to handle the problem.
}
finally{
//Closing resources etc.
}
Or You can write this :
try{
//code
}
catch(Exception ex){
//code to handle the problem.
}
Or this :
try{
//code
}
finally{
//Closing resources etc.
}
But, you usually would want to handle the problem with the catch block.

Catch Exception in finally { } ? Must?

I feel puzzle ...
I write a small routine in .jsp. Finally, ResultSet, Statement and Connection are required to be closed. I also write the closing codes in finally { }, but when the page is run, it return error that I didn't catch exception ...
I read some forum. Other people didn't catch any exception in finally { }
Any Hint ?
Sounds like you have the old problem of needing to close() in a finally block but close() throws an exception itself. Try somethig like the following...
ResultSet rs;
try {
// do various stuff
rs = ...;
} finally {
try {
if (rs != null) rs.close();
} catch (SQLException e) {
// do something with exception
}
}
You must catch exceptions in the code finally block. As you must catch exceptions in the catch block. Nested try/catches are a regular thing (albeit ugly).
One important note here is that you could have the exceptions that occur in finally declared in the throws clause of the method. However that would lead to the exception in finally overriding the original exception, which is lost. And you will see, for example, a NullPointerException, rather than FileNotFoundException.
By the way, avoid having code in the JSP file. Place it in a servlet.
finally{} doesn't do any exception catching. A finally{} block exists to make sure that certain code is run, no matter whether the try{} block reached its natural end or if it's jumping temporarily to the finally{} because an exception happened and that finally{} block was along the way. But after the finally{} finishes, the exception goes about its merry business, cavorting its way up the stack and cheerfully crashing your program.
If you want to actually catch the exception and stop it from unwinding the stack further, use catch(){}. But don't use catch blindly- catching an exception you don't actually know how to recover from is much worse than crashing, because now your program isn't working correctly and you don't have an exception stack trace telling you why.
Your ResultSet, Statement, and Connection almost certainly did get closed. And then the exception continued happening and crashed your program anyway, because that had nothing to do with your ResultSet, Statement, and Connection.
What was the actual exception?
Maybe I'm getting old, but what's wrong with catching exceptions in the catch block?
It helps if you say what is in your try block. You are probably not catching appropriate exception or your code in finally throws exception.
It is OK to have finally without catch.
try {
//do some work
}
finally {
//check of state and do clean up. You would have reached here via multiple branches.
}
It more appropriate to catch specific exceptions using catch and then handle specific cleanup there. Use finally for any code that must get executed even when exception happen.
try {
//do some work
}
catch ( RecoverableException1 re1) {
//cleanup
}
catch ( RecoverableException2 re2) {
//cleanup
}
finally {
//check of state and do clean up. You would have reached here via multiple branches.
}
finally{
try{
resultSet.close();
}catch(E e){
}finally{
try{
statement.close();
}catch(E e){
}finally{
conn.close();
}
}
}

Categories