Exception vs Error in Java

I will tell you here what are the differences between Exception and Error in Java.

There are mainly three important categories of Throwable:

  • Error – something severe enough has gone wrong the most applications should crash rather than try to handle the problem.
  • Unchecked Exception (also known as, RuntimeException) – mainly a programming error, such as, a NullPointerException or an illegal argument. Applications can sometimes handle or recover from this Throwable category or at least catch it at the Thread’s run() method, log the complaint, and continue running.
  • Checked Exception (everything else) – applications are expected to be able to catch and meaningfully do something with the rest, such as FileNotFoundException and TimeoutException.

Below are the few points about Exception vs Error:

An Exception indicates conditions that a reasonable application might want to catch.

Therefore, checked exceptions are generally those from which a program can recover & it might be a good idea to recover from such exceptions pro-grammatically. A programmer is expected to check for these exceptions by using the try-catch block or throw it back to the caller.

Unchecked exceptions are those exceptions that might not happen if everything is in order, but they do occur. Many applications will use try-catch or throws clause for RuntimeExceptions and their sub-classes but from the language perspective it is not required to do so.

An Error indicates serious problems that a reasonable application should not try to catch. Errors tend to signal the end of your application. It typically cannot be recovered from and should cause your JVM to exit. Catching them should not be done except to possibly log or display and appropriate message before exiting. For example, OutOfMemoryError.

Related Posts:

Below are the few of the sub-classes of Error, you can find them in 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 – sub-classes 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.

Both Error and Exception extend Throwable.

Though even application can raise an Error but it is just not a good practice, instead application should use checked exceptions for recoverable conditions and runtime exceptions for programming errors.

In general error is which nobody can control or guess when it would occur. For example, OutOfMemoryError, which nobody can guess and can handle it.

Exception can be guessed and can be handled in a program. For example, if your code is looking for a file which is not available then IOException is thrown. Such instances programmer can guess and can handle it.

That’s all about Exception vs Error.

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *