• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

When the Statment After Finally clause will get Executed

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ---");
}
}
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 80138
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic