• Post Reply Bookmark Topic Watch Topic
  • New Topic

Catching Exception vs Throwable  RSS feed

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am coming back to java after sometime and wanted to know if I want to do a catch all exception should I use catch (Exception e) or catch (Throwable t). What are the differences and pros and cons of them
 
Saloon Keeper
Posts: 7994
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You catch exceptions that you want to deal with specifically. Never catch Exception, RuntimeException, Error or Throwable unless you have a REALLY good reason to (such as preventing rogue plugins from crashing the rest of the application).

That means that most of the time, you only have to catch checked exceptions, which you should try to do using the most specific type. If there are multiple disjoint exceptions that you want to handle the same way, use a disjoint catch clause:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!