This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error Handling in Java (Exception v/s Errors)

 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Can we catch an Error in Java application?

2. Should we catch the Error in Java app? If not, then how we should handle the Error situations?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. You can catch anything that extends java.lang.Throwable, and java.lang.Error indeed extends Throwable - so yes, you can catch Errors. Note that you could have found the answer yourself by quickly writing a small program to try it out. Experimenting is a great way to learn new things.

2. No, you should not normally catch Errors. Note that the API documentation of java.lang.Error says this:
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.

 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaibhav G Garg wrote:If not, then how we should handle the Error situations?

And the reason for not catching them is that Errors are usually caused by something your application simply cannot handle effectively, like being out of memory. So just let it happen.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic