DEV Community

Cover image for Difference between error and exception in java
Lalit Kumar
Lalit Kumar

Posted on

Difference between error and exception in java

In ths article, we will learn the major “Differences Between Error and Exception in Java”
"Throwable" as root for errors and Java exception hierarchy. "Error" is a critical condition that can not be handled by the program code. "Exception" is an exceptional situation that can be handled by the program code.
The significant difference between error and exception is that the error is caused due to lack of system resources, and exceptions caused by your code. Let us learn another difference between mistakes and exceptions .

Definition of Errors

"Error" is a subclass of the built-in class "Throwable" it. The error is a critical condition that occurs due to lack of system resources, and can not be handled by the program code. Mistakes can not be restored by any means because they can not be created, removed, captured or answered. Errors caused by a catastrophic failure which can not usually be handled by your program.
Error type always checked, because the compiler has no knowledge of the incident. Errors always occur at run-time environment. Errors can be explained by way of example, the program has an error of stack overflow, out of memory errors, or errors system crashes, this type of error is due to the system.
This code is not responsible for the error. The consequences of the occurrence of the error is that the program will be terminated abnormally.

Definition of Exception

"Exception" is also a subclass of the built-in class "Throwable". Exceptions are exceptional conditions that occur in the runtime environment. Most of the time exception because the code of our program. However, exceptions can be handled by the program itself, as an exception can be restored. Exceptions are handled by using three key words "try", "catch", "throw". Syntax of exceptions are:

  1. try {
  2. // write your code here 3.} catch (Exception type) {
  3. // write your code here 5.} In the above code, code written in the try block is the code that you want to monitor for exceptions. If an exception occurs in the try block, it is thrown using the "throw" keyword. Exceptions thrown can be caught by "catch" block of code above. "Exception type" is the type of exception that has occurred. In simple words we can say that the error occurs because the code is inappropriately called exceptions. For example, if the class was not found, or the requested method was not found. The types of exclusion is because the code in this program; This system is not responsible for the kinds of exceptions. Exceptions are classified as "checked" and "unchecked". unchecked exceptions are not in the knowledge of the compiler because they occur during runtime while, the compiler has knowledge of checked exceptions because they are known to the compiler during compile time. Read more

Top comments (0)