• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error Handling in Java (Exception v/s Errors)

 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • 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
Posts: 15278
39
Android IntelliJ IDE Java Scala Spring
  • 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: 4567
8
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic