• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how ro handle exceptions in finally block .please help me thanks inadvance

 
raja pulleboina
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how ro handle exceptions in finally block .please help me thanks inadvance
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally block is meant for cleanup.. and there is a rare possiblity of getting an exception there.. many cases it can be ignored.
If required we can handle that exception.. the similar way of putting that code inside the try/block.

If we are ignoring the exception inside the finally block, it is better to atleast log the stack trace.
 
raja pulleboina
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi chinna,

thanks for your update but if we have any exception in finally block how the exception will catch as the finally block is hte last executer for that thread....
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi raja,

you will put the statement causing exception in try/catch block inside the finally block that is you will handle the exception normally as you do with exception outside the finally only thing is you are catching it within the finally block.

example:
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chinna Eranna wrote:Finally block is meant for cleanup.. and there is a rare possiblity of getting an exception there..

Closing streams, database connections etc are usually done in a finally block, and the close() method of all these can throw a checked exception. It's not so rare as you think.

You are right that in most of those cases all you can do is log the exception.
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Closing streams, database connections etc are usually done in a finally block, and the close() method of all these can throw a checked exception. It's not so rare as you think.


I said getting an exception.. is rare.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic