• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception

 
Shiaber Shaam
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........
 
joshua antony
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.
 
Ramasubbhu Allur Kuppusamy
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....
 
wise owen
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.
 
Anupama Ponnapalli
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.
 
S Thiyanesh
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....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic