• Post Reply Bookmark Topic Watch Topic
  • New Topic

about exception handling  RSS feed

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
respected folks...
i have a try ,catch and finally block
i write a return statement in catch block aswell as in finally block .
which return statement exicutes?
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always the return statement in the finally block executes irrespective of whether exception is thrown or not.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The finally block will get executed even if we give a return statement in the try or catch block. So the return statement of finally will be executed i beleive.
 
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



The code return 3. You will get a warning If you compile this.

// Mathias
 
John David
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you explain why the return statement in finally block only exicuted?
what happen when the controller encounters the return statement in catch block?
 
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was a similar discussion about this here
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If a finally clause is included, it's statements are executed after all other try-catch processing is complete.
The finally clause executes whether or not an exception is thrown or a break or continue are encountered

The finally(if used) is always executed when there is a try catch block. The only fail condition for this is if you write a System.exit() in a catch block which will fail finally to execute.
 
John David
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
respected jai kiran ..... excellent answer. thanks for your great responce..
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!