Difference Between An Error And Exception in Java

Introduction

 
In this article, we discuss the differences between exceptions and errors in Java. Both errors and exceptions are derived from "java.lang.Throwable" in Java.
 

Difference between exception and error in Java

  • Exceptions are related tothe application and an Error is related to the environment in which theapplication is running.
  • An Error can't be recovered as it is fatal in nature, that isdifferent in the case of an Exception that may not be fatal in all cases.
  • An Exception is basicallydivided into two categories, i.e. Checked and Unchecked Exceptions. AChecked Exception has a special place in the Java programming language andrequires a mandatory try catch finally code block to handle it. On the otherhand an Unchecked Exception is a subclass of Runtime Exception that usuallyrepresents programming errors.
  • Exceptions can be checked orunchecked to indicate an error caused by the programmer should be handled at the application level. Whereas errors are always unchecked and usuallyindicate a system error or a problem with a low-level resource should behandled at the system level, if possible.
  • Like Exceptions, Errors inJava is also unchecked. The compiler will not throw a compile-time error if it doesn't see an Error handled with a try-catch or finally block. In facthandling an Error is not a good idea because recovery from an Error isusually not possible.

Error in Java

 
When a dynamic linking failure or other hard failures in the Java Virtual Machine occurs, the virtual machine throws an Error. Simple programs typically do not catch or throw Errors.
 
A few examples causing Java Runtime Errors are:
  1. Using a negative size in anarray.
  2. Conversion of a string into anumber.
  3. Divide an integer by zero.
  4. Access an element that is out of bounds of an array
  5. Store a value that is ofincompatible data type in an array.
  6. Cast an instance of baseclass to one of its derived classes.
  7. Using a null object toreference an object's member.
  8. Capitalization of keywords.
  9. Missing brackets in ano-argument message.
  10. Writing the wrong format fora class method, etcetera.
Example
 
In this example, we show different errors.
  1. class MyErrorClass {  
  2.  void fun1() {  
  3.   int y = 102;  
  4.   // Error, closing brace is missing  
  5.   void fun2() {  
  6.    int x = 1220;  
  7.   }  
  8.  }  
  9. }  
Output
 
Fig-1.jpg
 

Exception in Java

 
An exception is called and even occurs during the execution of a program, that disrupts the normal flow of the program's instructions.
When an exception occurs within a method, the method creates an object and hands it off to the runtime system. Creating an exception object and handing it to the runtime system is called throwing an exception.
 
It can occur for any of the following reasons:
  • Due to entering invalid data.
  • Due to the loss of network connection in themiddle of communications.
  • Due to missing a required file.

Exception types in Java

 
Exceptions are divided mainly into the following three types:
  1. Checked
  2. Runtime/unchecked
  3. Error
Let's take some exception
 
1. ArithmeticException
 
If we divide by zero, there occurs an ArithmeticException.
 
int x=12230/0;//ArithmeticException
 
2. NullPointException
 
If we provide a null value in any variable and performing some task, that causes an NullPointException.
 
String str=null;

System.out.println(str.length()); //NullPointException
 
3. NumberFormatException
 
The wrong/different formatting of any value, may cause a NumberFormatException.
 
String str="xyz";

int x=Integer.parseInt(str); //NumberFormatException
 
4. ArrayIndexOutOfBoundsException
 
If we use a value that is an incorrect index then that causes an ArrayIndexOutOfBoundsException.
 
int x[]=new int[5];

x[10]=1223; //ArrayIndexOutOfBoundException
 
Example
 
In this example, we saw an ArithmeticException and use try-catch to handle this exception.
  1. class ExceptionEx  
  2. {  
  3.  public static void main(String[] args)  
  4.  {  
  5.   try  
  6.   {  
  7.    int result = 1243443 / 0;  
  8.   } catch (ArithmeticException ae)  
  9.   {  
  10.    System.out.println(ae);  
  11.   }  
  12.   System.out.println("rest code");  
  13.  }  
  14. }  
Output
 
Fig-2.jpg