“Throwable” act as the root for Java’s error and exception hierarchy. “Error” is a critical condition that cannot be handled by the code of the program. “Exception” is the exceptional situation that can be handled by the code of the program.
The significant difference between error and exception is that an error is caused due to lack of system resources, and an exception is caused because of your code. Let us study other differences between error and exception along with a comparison chart.
Content: Error Vs Exception
Comparison Chart
Basis for Comparison | Error | Exception |
---|---|---|
Basic | An error is caused due to lack of system resources. | An exception is caused because of the code. |
Recovery | An error is irrecoverable. | An exception is recoverable. |
Keywords | There is no means to handle an error by the program code. | Exceptions are handled using three keywords "try", "catch", and "throw". |
Consequences | As the error is detected the program will terminated abnormally. | As an exception is detected, it is thrown and caught by the "throw" and "catch" keywords correspondingly. |
Types | Errors are classified as unchecked type. | Exceptions are classified as checked or unchecked type. |
Package | In Java, errors are defined "java.lang.Error" package. | In Java, an exceptions are defined in"java.lang.Exception". |
Example | OutOfMemory, StackOverFlow. | Checked Exceptions : NoSuchMethod, ClassNotFound. Unchecked Exceptions : NullPointer, IndexOutOfBounds. |
Definition of Error
“Error” is a subclass of the built-in class “Throwable”. Errors are the critical conditions that occur due to the lack of the system resources, and it can not be handled by the code of the program. Errors can not be recovered by any means because they can not be created, thrown, caught or replied. Errors are caused due to the catastrophic failure which usually can not be handled by your program.
Errors are always of unchecked type, as compiler do not have any knowledge about its occurrence. Errors always occur in the run-time environment. The error can be explained with the help of an example, the program has an error of stack overflow, out of memory error, or a system crash error, this kind of error are due to the system.
The code is not responsible for such errors. The consequence of the occurrence of the error is that the program gets terminated abnormally.
Definition of Exception
“Exception” is also a subclass of built-in class “Throwable”. Exceptions are the exceptional conditions that occur in a runtime environment. Most of the times exceptions are caused due to the code of our program. But, exceptions can be handled by the program itself, as exceptions are recoverable. Exceptions are handled by using three keywords “try”, “catch”, “throw”. The syntax of writing an exception is:
try { //write your code here } Catch (Exception type) { //write your code here }
In above code, the code written in the try block is the code which you want to monitor for the exception. If the exception occurs in a try block, it is thrown using the “throw” keyword. The exception thrown can be caught by the “catch” block of the above code. “Exception type” is the type of the exception that has occurred.
In simple words we can say that the mistakes occurred due to the improper code are called exceptions. For example, if a requested class is not found, or a requested method is not found. These kinds of exceptions are due to the code in the program; the system is not responsible for these kinds of exceptions.
The exceptions are classified as the “checked” and “unchecked”. Unchecked exceptions are not in knowledge of compiler as they occur during runtime whereas, the compiler has the knowledge about checked exceptions as they are known to compiler during compile time.
Key Differences Between Error and Exception
- Error occur only when system resources are deficient whereas, an exception is caused if a code has some problem.
- An error can never be recovered whereas, an exception can be recovered by preparing the code to handle the exception.
- An error can never be handled but, an exception can be handled by the code if the code throwing an exception is written inside a try and catch block.
- If an error has occurred, the program will be terminated abnormally. On the other hand, If the exception occurs the program will throw an exception, and it is handled using the try and catch block.
- Errors are of unchecked type i.e. error are not in the knowledge of compilers whereas, an exception is classified as checked and unchecked.
- Errors are defined in java.lang.Error package whereas, an exception is defined java.lang.Exception.
Conclusion
Exceptions are the results of mistakes done in the coding of the program, and the errors are the result of the improper functioning of the system
M.Anusha says
The information provided in this site is in a very simplified manner and useful 👌👍