File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
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: 141
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: 15037

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: 4541

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:
subject: Error Handling in Java (Exception v/s Errors)
It's not a secret anymore!