Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Exception

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When will the code after finally block gets executed... In some cases it does not get executed and some times gets executed.... Plz resolve this with examples if u can........
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ex: if you have a return statement in try or catch block , the finally block will be executed and the control goes to the calling method.
In that case code after finally will not be executed,else it will be executed.
Ex:
try{
return "something";---control goes back to the calling method
}
finally{
System.out.println("finally block");
}
............/*after finally code--in this example this code will not be
executed.*/
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply... If u can explain other cases which dont have return stmt and all.....
 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we do not have return statement then the code below finally will be executed(of course if we do not have return statement in finally itself)
Read K&B , it is really good
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check it Out..
try{ //throws some eccpn }
catch(Excpn e){}
finally{}
//Some Code after finally...


My Queries...
if there occurs excpn and wat happens...
if there is no catch block and there if occurs excpn wat happens....
 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there is exception , the catch block catches and handles the exception.
If there were no catch block, the finally block is executed and the exception is thrown.

Note that finally block executes after the exception is thrown but before the exception is executed if there is no catch block.
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shiaber,
Finally block will be executed, no matter what!
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have confusions whether the code after finally block will execute or not... in some book i read that if the excpn is not handled(ie no catch block)... the finally will get executed and the pgm terminates without executing the code after finally block....

Am i right....
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling System.exit() an prevent a finally block from executing. It will shut down the virtual machine.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Shiaber M:
in some book i read that if the excpn is not handled(ie no catch block)... the finally will get executed and the pgm terminates without executing the code after finally block....

Am i right....



Thats correct, if an exception is not caught, the finally block will execute (since its guaranteed to). but, the code in the method after the finally block will not be executed.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code after Finally block executes when
When program runs without any exceptions.
When the exception is caught in any of the catch.
When there is no return statement in try or catch or finally.
When the exceptions are not rethrown.
When System.exit() is called
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well said... Thaynesh..........
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But ...When System.exit() is called, I think it wont get executed....
 
Always look on the bright side of life. At least this ad is really tiny:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!