Reputation: 32807
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
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:
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;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
);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
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
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
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
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