finally block in Exception

M

Matt

I have question on finally block. If exception is catched, the statements inside
finally block and beyond finally block still will execute. Then what's the point
to put finally keyword?? Please advise. Thanks!!


C:\>java ExceptionTest3
abc.txt (The system cannot find the file specified)
inside finally block...
beyond finally block...

--------------------------------------------------------
import java.io.*;

public class ExceptionTest3
{ public static void openFile() throws IOException
{ BufferedReader br = new BufferedReader(new FileReader("abc.txt"));
String line = br.readLine();
br.close();
}

public static void main(String args[])
{ try
{ openFile();
}
catch(Exception e)
{ System.out.println(e.getMessage());
}
finally
{
System.out.println("inside finally block...");
}
System.out.println("beyond finally block...");
}
}
 
J

Joona I Palaste

Matt said:
I have question on finally block. If exception is catched, the statements inside
finally block and beyond finally block still will execute. Then what's the point
to put finally keyword?? Please advise. Thanks!!

I believe the purpose of the finally block is to ensure the code inside
it *will* get executed no matter what happens: the try block completes
normally, the try block throws an exception, or even the try block
explicitly returns from the method.
 
X

xarax

Joona I Palaste said:
I believe the purpose of the finally block is to ensure the code inside
it *will* get executed no matter what happens: the try block completes
normally, the try block throws an exception, or even the try block
explicitly returns from the method.

Furthermore, an exception is never catched. An exception is caught.

Also, the catch(....) block may miss unchecked exceptions
and the finally block is there to be sure that some code
is executed before the stack is unwound to the calling
method.
 
S

Sudsy

Matt said:
I have question on finally block. If exception is catched, the statements inside
finally block and beyond finally block still will execute. Then what's the point
to put finally keyword?? Please advise. Thanks!!

You've caught the exception but done nothing with it. Try modifying the
code so that main throws IOException and remove the catch block. You'll
find that the finally block executes but nothing beyond.
 
A

Adam Maass

Matt said:
I have question on finally block. If exception is catched, the statements inside
finally block and beyond finally block still will execute. Then what's the point
to put finally keyword?? Please advise. Thanks!!

Use finally to clean up resources, even if code generates an exception:

public void foo() throws MyException
{
Integer i = null;

try
{
i = new Integer(0);
throw new MyException();
}
finally
{
i = null;
}
}


Not so interesting with an Integer, but much more interesting with
java.sql.Statement and Connection objects.

-- Adam Maass
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top