Anirudha
Anirudha

Reputation: 32807

Why do we need Error class?

We have Throwable class which is the base class for Error class (for unrecoverable errors) and Exception class(for recoverable errors)

So,

1> we can throw an object that implement error class.(Although it doesn't make sense to implement Error class because we have Exception class to do the same thing..)

2> Java doesn't recommend to catch Error object..


So what is the need of Error object then? Cant the compiler implement it internally? Isn't it a mistake?

Upvotes: 4

Views: 11918

Answers (4)

fge
fge

Reputation: 121740

Technically, the distinction is not really made between "unrecoverable error" and "recoverable error", but between checked exceptions and unchecked exceptions. Java does distinguish between them as follows:

  • you must declare a checked exception in your throws clause; if using a method which throws a checked exception in a try block, you must either catch said exception or add this exception to your method's throws clause;
  • you may declare an unchecked exception in your throws clause (not recommended); if using a method which throws an unchecked exception in a try block, you may catch that exception or add this exception to your method's throws clause (not recommended either).

What is certainly not recommended, unless you really know what you are doing, is to "swallow" any kind of unchecked exception (ie, catch it with an empty block).

Exception is the base checked exception class; Error and RuntimeException are both unchecked exceptions, and so are all their subclasses. You will note that all three classes extend Throwable, and the javadoc for Throwable states that:

For the purposes of compile-time checking of exceptions, Throwable and any subclass of Throwable that is not also a subclass of either RuntimeException or Error are regarded as checked exceptions.

Classical examples of (in)famous unchecked exceptions:

  • OutOfMemoryError (extends Error);
  • StackOverflowError (extends Error);
  • NullPointerException (extends RuntimeException);
  • IllegalArgumentException (extends RuntimeException);
  • etc etc.

The only real difference between Error and RuntimeException is their estimated severity level, and is a "semantic" difference, not a technical difference: ultimately, both behave the same. Some IDEs (Intellij IDEA comes to mind) will also yell at you if you catch an Error but do not rethrow it.

Upvotes: 9

Rahul
Rahul

Reputation: 16335

javadoc for error says

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.

whereas for Exception, the javadoc says

The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.

Some Differences

  1. Error is not meant to be caught, even if you catch it you can not recover from it. For example during OutOfMemoryError, if you catch it you will get it again because GC may not be able to free memory in first place. On the other hand Exception can be caught and handled properly.
  2. Error are often fatal in nature and recovery from Error is not possible which is different in case of Exception which may not be fatal in all cases. Difference between Error and Exception in Java
  3. Unlike Error, Exception is generally divided into two categories e.g. checked and unchecked Exceptions. Checked Exception has special place in Java programming language and require a mandatory try catch finally code block to handle it. On the other hand Unchecked Exception, which are subclass of RuntimeException mostly represent programming errors. Most common example of unchecked exception is NullPointerException in Java.
  4. Similar to unchecked Exception, Error in Java are also unchecked. Compiler will not throw compile time error if it doesn't see Error handled with try catch or finally block. In fact handling Error is not a good Idea because recovery from Error is mostly not possible.

That's all on difference between Error and Exception in Java. key point to remember is that Error are fatal in nature and recovery may not be possible, on the other hand by carefully handling Exception you can make your code more robust and guard against different scenarios.

Look at a few of the subclasses of Error, taking from their respective javadoc:

AnnotationFormatError - Thrown when the annotation parser attempts to read an annotation from a class file and determines that the annotation is malformed.

AssertionError - Thrown to indicate that an assertion has failed.

LinkageError - Subclasses of LinkageError indicate that a class has some dependency on another class; however, the latter class has incompatibly changed after the compilation of the former class.

VirtualMachineError - Thrown to indicate that the Java Virtual Machine is broken or has run out of resources necessary for it to continue operating.

Upvotes: 5

RB.
RB.

Reputation: 37192

From the Error documentation:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.

and the Exception documentation:

The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.

I think this makes the distinction clear. Note also that both inherit from throwable and so can both be thrown.

Upvotes: 3

dty
dty

Reputation: 18998

You certainly can throw objects that extend (not implement) the Error class.

As you said, Error exists for unrecoverable errors. The most extensive use is within the JVM itself which uses subclasses of Error for things it can't recover from and doesn't expect you to be able to recover from - such as running out of memory.

Upvotes: 7

Related Questions