SEMrush

Please try to remember these TOP few Differences between Checked and UnChecked Exceptions




Hierarchy of Exceptions 


Unchecked Exceptions 

Unchecked exceptions are nothing but Runtime Exceptions. Since they are not checked at compile time, so they are called Unchecked Exceptions

Following Exceptions appear in the category of Unchecked exception
  • ArithmeticException
  • ArrayIndexOutOfBoundsException
  • NullPointerException

Runtime Exceptions are the ones that are not checked at compile time. Rather they appear at the runtime .

  • ArithmeticException - dividing a number from ‘0’ throws  Exception in thread "main" java.lang.ArithmeticException: / by zero
  • ArrayIndexOutOfBoundsException - Trying to access an index that does not exists  
  int arr[] = {'0','1','2'};
                                      System.out.println(arr[4]);
            
  • NullPointerException - Trying to access a null object
String string = null;  
System.out.println(string.length());


Checked Exceptions 

Exceptions that are checked at compile-time are called checked exceptions
  • FileNotFoundException
  • ParseException
  • ClassNotFoundException
  • CloneNotSupportedException
  • InstantiationException
  • InterruptedException
  • NoSuchMethodException
  • NoSuchFieldException

In certain situations (In case of Checked Exceptions) Java compiler force the programmer to write a handling code to deal with exceptions. For example let’s assume we are having a code that reads from an external file, JVM expects some handling code for the situation if the required file is not available at the run time. This is how Java maintains its integrity against code failure by allowing the programmer to write handling code for failure prone areas of code.


These are the ones for which we have already written  Exception handler in the code at compile time, the Exceptions thrown in such situation are called Checked Exception.




Comments