In Java, java.lang.Exception and java.lang.Error belong to the class java.lang.Throwable class. However, there are some basic differences that exist in between these two sub-classes. While errors are represented by the java.lang.Error class, especially the ones attributed to the runtime environment of the application, java.lang.Exception class considers the exceptions attributed to the application itself. In this article, we shall discuss the definitions and features of Error and Exception in Java, the difference between Exception and Error, etc. Read on to begin with a comparative chart showcasing the main differences between Error and exception in Java.
Error vs Exception
Basis of differentiation |
Error |
Exception |
Type |
Errors representing Java are of the type java.lang.Error. |
Exceptions represented by Java are of the type java.lang.Exception. |
Checked or unchecked |
The Errors found in java are of the unchecked type. |
The Exceptions found in java are of both types – unchecked and checked. |
Timing |
In Java, the Errors occur at run time. These errors cannot be found by the compiler. |
The checked exceptions in Java are in the knowledge of the compiler. However, the compiler fails to have any information of the unchecked exceptions as they take place at run time. |
Possibility of recovery |
It is not possible or Java programs to gain recovery from errors. |
Proper handling with the help of try-catch blocks can help programs recover from Exceptions in Java. |
Caused by |
Errors are mainly attributed to the runtime environment of an application. |
The application itself is the main cause of Exceptions. |
Examples |
java.lang.OutOfMemoryError and java.lang.StackOverflowError |
Checked Exceptions can be found as IOException, SQLException, etc. |
What is Error in JAVA
An Error in Java is usually indicative of a serious problem that can be fatal, and it is best for a reasonable application to not attempt to catch the same. It is a subclasses belonging to the java.lang.Throwable class in Java. As Errors pertain to conditions that are impossible to recover from via the usual handling techniques, abnormal termination of a program/ application serves to be the only way out. The Errors in the unchecked type category are mostly found at runtime. The striking instances of Errors in Java are Out of Memory Error, System Crash Error, etc.
What is Exception in JAVA
Exceptions in Java are events occurring during the time of program execution. Once they take place, the normal flow of related instructions is disrupted in the form of “array access out of bound”, “divide by zero”, etc. coming to the fore as Exceptions. An Exception in java relates to an object encapsulating an error event that takes place within a method; it usually contains the following:
1) Information pertaining to the error along with its type
2) Other customized information
3) The status of the application/ program at the time of the error taking place, etc.
The Exception objects are capable of being thrown and caught. They are useful for indicating the different kinds of error conditions. Some examples of Exceptions are FileNotFoundException, SocketTimeoutException, IOException, ArrayIndexOutOfBoundsException, NullPointerException, ArithmeticException, etc.
Key Difference between Exception and Error
The key differences between Exception and Error in Java are enumerated below:
1) Recovery from errors is impossible in Java programs. The only way to handle an error is by terminating the execution of the application. On the other hand, it is possible for an application to recover from an exception by throwing the exception back to the caller, or via try-catch blocks.
2) Errors in Java are incapable of being handled with the help of try-catch blocks. Even if the try-catch blocks are used for the purpose, the application will fail to recover. Conversely, once the try-catch blocks are used for handling Exceptions the program starts flowing normally once they happen.
3) The Errors in Java are undivided and belong to a singular category – checked. On the other hand, there are two categories of Exceptions in Java- Checked and unchecked.
4) As the sub classes of RunTimeException and Errors in Java are prone to taking place during runtime, the compiler fails to have any knowledge about their existence.
Therefore, the unchecked exceptions and Errors are likely to go unnoticed by the compiler. On the other hand, the information about checked Exceptions is present with the Java compiler. In turn, the compiler forces the user to implement try-catch blocks on the lines that may throw checked exceptions.
Conclusion
Error and Exception are both subclasses of Class Throwable. Regardless of this, in most cases, an Error is thrown back by the JVM. The scenario created is fatal as the application or program will not be able to recover from that Error e.g. OutOfMemoryError. A checked Exception comes to the knowledge of the Java compiler and can be handled. In case you have any further questions with respect to the differences between Error and Exception handling in Java then do get back to us in the Comments section below. We shall provide answers to your concerns at the earliest.