File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes When the Statment  After Finally clause will get  Executed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When the Statment  After Finally clause will get  Executed" Watch "When the Statment  After Finally clause will get  Executed" New topic
Author

When the Statment After Finally clause will get Executed

praveen kumar gowda
Greenhorn

Joined: Feb 20, 2012
Posts: 23
Hi..this is the code, where i have statement after finally...if no exception...the statment will work fine .....

i have doubt in two conditon....wheather the statment get executed

1)when the exception occur in openFile method
2)when the exception occur in finally clause

public void openFile() throws IOException {
FileReader reader = null;
try {
reader = new FileReader("someFile");
int i=0;
while(i != -1){
i = reader.read();
System.out.println((char) i );
}
} finally {
if(reader != null){
try {
reader.close();
} catch (IOException e) {
//do something clever with the exception
}
}
System.out.println("--- File End ---");
}
}
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

praveen kumar gowda wrote:i have doubt in two conditon....wheather the statment get executed

I assume you meant to say statement(s) after finally block.

Well, programmatically, only thing which can stop finally block from execution is - if try and/or catch block contains a call to system.exit and that call is getting executed (e.g. if catch contains system.exit, but there's not exception, then finally block will be executed). There are non-programmatic things which can prevent finally block from getting executed - like JVM crash, OS crash, catastrophic failure, non-maskable interrupt (say power supply is disconnected) etc.

Now, about statement(s) after finally block:
1) If exception(s) occur in finally block, and those exceptions are handled, and no new exceptions are thrown from catch block, then those statements(statements after finally block) get executed.
2) If exception(s) occur before finally block, and those exceptions are handled, and no new exceptions are thrown from catch block, then those statements(statements after finally block) get executed.

In your code, you are not catching any exception in first catch block. So, if any exception occurs there, statements after finally block will not be executed.

In your finally block, you are handling only IOException. So, if any other exception occurs, then statements after finally block will not be executed.

By the way (its not related to your question, but) if your openFile says that it throws IOException, then inside instead of doing something clever, you can simply
I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Please use code tags; you can go back and edit your post, which is difficult to read without them.
Why on earth are you using the read() method. Why are you not buffering your input? Look at this post of Rob Spoor’s, which shows how to do it. You can alter that code to insert something after the finally, and inside the outer try , and see what happens.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When the Statment After Finally clause will get Executed