Java

What is the Difference Between Errors and Exceptions in Java

While programming in Java, there can be situations where the developer comes across various limitations. These bottlenecks can be in the form of “errors” or “exceptions”. The former ones halt the code functionalities, thereby refraining the programmer to move on with the code. The exceptions, however, can be avoided to save time and hassle.

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:

int a = 2

System.out.println(a);

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:

class Parent {

 public void display() {

System.out.println("Linuxhint!");

}}

class Child extends Parent {

 public void display() {

   display();

System.out.println("Java Programming!");

}}

public class errordemo {

 public static void main( String args[] ) {

  Child obj = new Child();

obj.display();

}}

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”:

try {

  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.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.