There are two types of exception available in java:
- Checked exception
- Unchecked exception
Checked exception:
- Checked exception represents the invalid condition is program and must be declared by using keyword throws in method.
- checked exception extends java.lang.Exception class.
- 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:
- 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.
- Unchecked Exception extends java.lang.RuntimeException class.
- ArithmaticException, NullPointerException, IndexOutOfBoundsException 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.
Leave a Reply