Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Types of Exception

There are mainly two types of exceptions: checked and unchecked where error is considered as unchecked exception. The sun Microsystems says there are three types of exceptions:

  • • Checked Exception
  • • Unchecked Exception
  • • Error

Checked Exception

  • • The Exceptions which are checked by the compiler for smooth execution of the program at run time are called Checked Exceptions.
  • Example;
    FileNotFoundException, IOException, SQLException
  • • In the case of Checked Exceptions, the compiler will check whether we are handling exception if the programmer not handling then we will get compile time error.

Let's see an example for better clarity.

class CheckedDemo
	public static void main(String args[])
		PrintWriter pw=new PrintWriter("f.txt");

unreported exception FileNotFoundException; must be caught or declared to be thrown

Here when we are compiling this program, the compiler generated a report which represents that there may be a chance FileNotFoundException arises at run time if the welcome.txt file is not found. So in this way the compiler gave this message to the programmer. This is known as checked exception.

Unchecked Exception

  • • The exceptions which are not checked by compiler are called as unchecked exceptions.

  • Example :

    ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.

  • • In the case of unchecked exceptions, the compiler wont check whether programmer handling exceptions or not.

  • Note :
  • • Whether the exception is Checked or Unchecked, compulsory it will occur only at run time. There is no chance of occurring any exception at compile time..
  • • Runtime exception and its child classes, Error and its child classes are Unchecked except this remaining are Checked exceptions.

  • Error :
  • • Most of the times errors are not caused by the programs, these are due to system lack of resources.
  • • Errors are non recoverable.

  • Example :
  • • If OutOfMemory error occurs being a programmer we can’t do anything and the program will be terminated abnormally.
  • • System admin or Server admin is responsible to increase heap memory.

About the Author

Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc