This article will discuss the differences between Java errors and exceptions.
What is the Difference Between Errors and Exceptions in Java?
The “Errors” in Java correspond to the critical cases that cannot be coped by the code. The “Exceptions”, in the other case can be detected and catered within the code to ensure the streamlining of the code:
Error | Exception |
Error cannot be recovered or handled. | The exceptions can be handled via the “try…catch” block. |
Errors are not known by the compiler. | Exceptions can or can not be identified by the compiler. |
Errors can be syntax or logical. | These can be unchecked or checked. |
Demonstration of “Syntax Error”
Firstly, let’s overview the occurrence of the “syntax” error:
In this code, simply initialize the integer without specifying the semi-colon “;” and display it.
Output
The discussed code will result in logging the displayed “syntax error” due to a missing semicolon.
Solution
As observed, by the placement of “;”, the faced error vanishes.
Demonstration of “Logical Error”
Now, let’s discuss the appearance of the “logical” error:
In this code snippet, apply the below-provided steps:
- Create a parent class named “Parent” and accumulate the function “display()” in it, displaying the provided message.
- In the next step, create a child class named “Child” inheriting the parent class via the “extends” keyword.
- Within this class, override the function identical to its parent class to access its functionalities.
- Lastly, in the “main”, create an object of the child class and invoke the overridden function which will display the “error” since no reference is passed.
Output
In this output, it can be seen that the stated limitation has been displayed. The “StackOverflow” error is usually caused by infinite recursion.
Solution
In this outcome, it can be implied that by associating the “super” keyword, the confusion between the identical child and parent class function is eliminated and so the error disappears.
Demonstration of “Exception”
Now, let’s discuss the following example stating the faced and handled “exception”:
int a = 2;
int b = a/0;
System.out.println(b);
}
catch (ArithmeticException e) {
System.out.println(e.getMessage());
}
In this code block:
- First, specify the “try” block. In this block, return the division of the initialized integers such that “infinity” is returned.
- This action results in returning an exception.
- To handle this exception, cater to the probable exception in the “catch” block.
- Within this block, display the exception message using the “getMessage()” method.
Output
From this output, it can be analyzed that the faced exception is handled appropriately.
Conclusion
The “Errors” in Java cannot be handled or resolved by the code whereas the “Exceptions” can be detected and handled making the code streamline. The errors become a bottleneck in continuing the functionalities while the exceptions can be avoided. This blog discussed the differences between the faced errors and exceptions in Java.