24

I am trying to understand the Error class in Java.

I have a good understanding of the Exception class, but can't find examples of code for the Error class. I've tried searching the web and also the java.sun website, but I'm not finding anything useful to help me understand this better.

How do I use the Error class in programs and where do we have to use that?

2
  • what is your exact requirement.can you detail it more. Commented Mar 7, 2011 at 9:06
  • i want to know the use of Error class and where we have to use that Commented Mar 7, 2011 at 9:30

3 Answers 3

36

You don't use Error in your code.

An Error is a specific kind of Throwable, just as Exception is.

  • Throwable is the base class that defines everything that can be thrown.
  • Exception is the common case. It is about problems that occur during the execution of your program.
    • RuntimeException is a special case: it's unchecked (i.e. it need not be declared by a method and the compiler doesn't force you to catch it).
  • Error is the "rare" case: it signifies problems that are outside the control of the usual application: JVM errors, out of memory, problems verifying bytecode: these are things that you should not handle because if they occur things are already so bad that your code is unlikely to be able to handle it sanely.

You should not attempt to correct the situation that resulted in an Error. You might want to catch it in order to log it and then rethrow it (see the JavaDoc of ThreadDeath for an example on why you need to rethrow it (thanks to @krock for the heads-up)).

There is no other reason to throw any Error (i.e. don't create an Error on your own and throw it, if you think you want to do that, use an Exception or a RuntimeException instead).

Sign up to request clarification or add additional context in comments.

6 Comments

...you could catch an Error... if you catch an Error you should generally rethrow it (see javadoc for ThreadDeath for one reason why)
btw I wasn't the downvoter, just adding my thoughts, +1 for a good summary of the exception hierarchy.
Thanks, krok, I rephrased the final bit to clarify the need to re-throw when an Error is caught.
@satheesh: while application code should not throw an Error, the JVM can (and will). Look at OutOfMemoryError. There's usually not much the application can do, if that happens. At best it can try to safe the ongoing work, but even that might fail (the JVM is out of memory after all!).
For those that use Eclipse, have you ever had it run out of memory? It essentially catches this, gives you the option of continuing or restarting. In the case of OutOfMemory your app might be able to free up enough to save critical work as a last ditch effort.
|
1

If you take a look at the Javadoc here there is a good explanation:

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.

Concerning the usage you also have this:

A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.

Comments

0

Error

Error a subclass of "Throwable" class is thrown by java runtime system and indicate some unrecoverable conditions during the execution of the programs Once thrown difficult to recover from it and the application get to halt. Eg..,java.lang.StackOverflowError and java.lang.OutofMemoryError

// An example code which throws StackOverflowError

public class ErrorDemo
   {
          public void method1()
            {
                   this.method2();
            }

          public void method2()
           {
                    this.method1();
           }

 public static void main(String sri[])
           {
                ErrorDemo k= new ErrorDemo();
                     k.method1();
            }
 }

In this code from main method we are calling method1 and from method1 a call was made to method2 and again from method2 we are calling method1 means we created a continous loop which doesn't goes to end and finally a critical error StackOverflowError is being thrown.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.