Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

about exception handling  RSS feed

 
John David
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?
 
Kamesh Rao
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.
 
Anu Pillai
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.
 
Mathias Nilsson
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?
 
Jaikiran Pai
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
 
Adhir Gupte
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..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!