wood burning stoves 2.0*
The moose likes Java in General and the fly likes catching RuntimeException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "catching RuntimeException" Watch "catching RuntimeException" New topic
Author

catching RuntimeException

Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

I have some code that I want to catch execptions in but I dont want to catch RuntimeExceptions. Is the code below the way to go, or should I be catching just the specific exceptions.
Findbugs flagged this for me as I didnt have the catch (RuntimeException rt) in orginally.

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

It's usually clearer to specify exactly which exceptions you do want to catch. I might change my mind if there were lots of them, though.
Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

Right so if im in a rush put in catch Exception but alos a catch RuntimeException and then rethrow it straight away.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Well, it should definitely work.

Except....don't throw a new RuntimeException. Throw the original, otherwise you're losing the information about it:

Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: catching RuntimeException
 
Similar Threads
Null Pointer exception
Exception
Hello help needed with extending AbstractSet
Exceptions thrown in interfaces and their implementations
Why is instanceof considered harmful?