Exception chaining in Java

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags with 4 Comments on Exception chaining in Java

Concept of Exception chaining with example in JAVA

Exception chaining is very powerful feature supported by the JDK but most of the developers are not able to use the advantage of Exception chaining in their application. Most of the developers have also seen the code in their existing project but unable to identify the advantage of exception chaining.

Definition: When one exception causes another exception then the second exception must print the log of the first exception also.

Exception Chaining
Exception Chaining

Continue reading “Exception chaining in Java”

Checked and Unchecked Exception

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags with 1 Comment on Checked and Unchecked Exception

Explains the concept of checked exception and unchecked exception in JAVA.

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.