This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Getting MEAN with Mongo, Express, Angular, and Node and have Simon Holmes on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Error Handling in Java (Exception v/s Errors) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error Handling in Java (Exception v/s Errors)" Watch "Error Handling in Java (Exception v/s Errors)" New topic

Error Handling in Java (Exception v/s Errors)

Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
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

Joined: Aug 16, 2005
Posts: 14741

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Matthew Brown

Joined: Apr 06, 2010
Posts: 4525

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.
It is sorta covered in the JavaRanch Style Guide.
subject: Error Handling in Java (Exception v/s Errors)