Checked and Unchecked Exception

There are two types of exception available in java:

  1. Checked exception
  2. Unchecked exception

Checked exception:

  1. Checked exception represents the invalid condition is program and must be declared by using keyword throws in method.
  2. checked exception extends java.lang.Exception class.
  3. IOException, ClassNotFoundException, SAXException are the well known examples of checked exception.

example:

public void setDate() throws ShivaSoftException
{
   if(dateOfBirth < todaysDate)
   {
        throw new ShivaSoftException("Date of birth cannot be in future");
   }
}

checked exception forces the calling method to be enclosed in try catch block.

Unchecked Exception:

  1. Unchecked exception represents the defect in programming logic and occurs at runtime. it does not forces the code to be enclosed in try catch block.
  2. Unchecked Exception extends java.lang.RuntimeException class.
  3. ArithmaticException, NullPointerExceptionIndexOutOfBoundsException are well known examples of Unchecked Exception.

example:

public void errorMethod()
{
   System.out.println(10/0);
}

The above method will give the ArithematicException at runtime but did not force to be enclosed during compile time.

Posted

in

by

Tags:


Related Posts

Comments

One response to “Checked and Unchecked Exception”

  1. […] the subclasses of Exception forms the “Checked Exception” which must be caught by catch […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading