Why do we need an error class?

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

So,

1> we can throw an object that implements the Error class. (Although it makes no sense to implement the Error class, because we have an Exception class to do the same.)

2> Java does not recommend catching an Error object.


So what does the Error object need then? Could the compiler inject it inside? Isn't that a mistake?

+4
source share
4 answers

Technically, the distinction is not really made between the "fatal error" and the "error being restored", but between checked exceptions and unchecked exceptions. Java distinguishes them as follows:

  • you must declare an exception in your throws ; if you use a method that throws a checked exception in a try block, you must either catch specified exception or add this exception to your throws method;
  • you can throw an unchecked exception in your throws (not recommended); if you use a method that throws an exception in the try block, you can catch exclude or add this exception to your throws method (not recommended).

What, of course, is not recommended if you really donโ€™t know what you are doing, is to " catch " any exception (for example, catch with an empty block).

Exception is a class of checked exceptions; Error and RuntimeException are both thrown exceptions, and they are all subclasses of them. You will notice that all three classes expand on Throwable , and the javadoc for Throwable states that:

For purposes of checking compile time exceptions, Throwable and any subclass of Throwable that is also not a subclass of RuntimeException or Error are considered checked exceptions.

Classic examples (c) of famous unchecked exceptions:

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

The only real difference between Error and RuntimeException is their estimated severity level and is a โ€œsemantic" difference, not a technical difference: in the end, both behave the same. Some IDEs (Intellij IDEA come to mind) will also shout at you if you catch Error but don't flip it.

+7
source

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

As you said, Error exists for fatal errors. The most widespread use is in the JVM itself, which uses Error subclasses for things from which it cannot recover and does not expect that you can recover it, for example, due to lack of memory.

+7
source

javadoc for error says

A bug is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most of these errors are abnormal conditions.

whereas for exception, javadoc says

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

Some differences

  • A mistake should not be caught, even if you catch it, you cannot recover it. For example, during an OutOfMemoryError , if you catch it, you will get it again, because the GC may not be able to free memory in the first place. On the other hand, an exception can be caught and handled correctly.
  • An error is often fatal in nature, and recovery from an error is not possible, which in case of exception cannot be fatal in all cases. Difference between error and exception in Java
  • Unlike an error, an exception is usually divided into two categories, for example. checked and unchecked Exceptions. Checked Exception has a special place in the Java programming language and requires the completion of the catch code to process it. Unchecked Exception, which is a subclass of RuntimeException, on the other hand, mostly represents programming errors. The most common example of a thrown exception is a NullPointerException in Java.
  • Like unchecked Exception, the error in Java is also not fixed. The compiler will not cause a compile-time error if it does not see an error processed with try catch or finally block. Actually handling Error is not a good idea, because recovering from an error is basically impossible.

This is all the difference between error and exception in Java. the key point to remember is that the Error is fatal in nature and it may not be possible to recover, on the other hand, by carefully processing the Exception, you can make your code more reliable and protect yourself from different scenarios.

Look at a few subclasses of Error, taking them from the corresponding javadoc:

AnnotationFormatError - thrown when the annotation parser tries to read the annotation from the class file and determine that the annotation is incorrect.

AssertionError - Discarded to indicate that the assertion failed.

LinkageError - Subclasses of LinkageError show that the class has some dependency on another class; however, the last class incompatibly changed after compiling the former class.

VirtualMachineError - selected to indicate that the Java virtual machine is broken or the resources needed to continue are running out.

+5
source

From the error documentation :

A bug is a Throwable subclass that points to serious issues that a reasonable application should not try to catch.

and Exclusion Documentation :

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

I think this makes the difference understandable. Also note that both inherit from throwable and therefore can be thrown.

+3
source

All Articles