wood burning stoves 2.0*
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
Author

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: 14114
    
  16

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 7 API documentation
Scala Notes - My blog about Scala
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4371
    
    8

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
 
subject: Error Handling in Java (Exception v/s Errors)