Exception Handling in Java

The exception handling in java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.

Other way The process of converting system error messages into user friendly error message is known as Exception handling. This is one of the powerful feature of Java to handle run time error and maintain normal flow of java application.

What is exception

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's Instructions.

What is exception handling

Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, Remote etc.

Advantage of Exception Handling

The core advantage of exception handling is to maintain the normal flow of the application. Exception normally disrupts the normal flow of the application that is why we use exception handling. Let's take a scenario:

statement 1;
statement 2;
statement 3;
statement 4;
statement 5;//exception occurs
statement 6;
statement 7;
statement 8;

Hierarchy of Java Exception classes

ecxeption1

Types of Exception

sun microsystem says there are three types of exceptions:

  • Checked Exception
  • Unchecked Exception
  • Error

Checked Exception

These are the exception are checked at compile-time. In general checked exception also can be handle by Exception class. The classes that extend Throwable class except Runtime Exception are known as checked exceptions.

  • Only for remember: Checked means checked by compiler so checked exception are checked at compile-time.
ecxeption

Un-Checked Exception

These are the exception both identifies or raised at run time, these can be handle using RunTimeException class.

  • Note: In real time application mostly we can handle unchecked exception.

    Only for remember: Un-checked means not checked by compiler so un-checked exception are checked at run-time not compile time.

ecxeption

Difference between checked Exception and un-checked Exception

Checked Exception Un-Checked Exception
1 checked Exception are checked at compile time. un-checked Exception are checked at run time.
2 for checked Exception Extend Throwable class except RuntimeException. for un-checked Exception extend RuntimeException.
3 e.g. IOException, SQLException, FileNotFoundException etc. e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, NumberNotFoundException etc.

Most important scenarios where exceptions may occur

They are as follows:

1 Scenario where ArithmeticException occurs

If we divide any number by zero, there occurs an ArithmeticException.

int a=50/0;//ArithmeticException

2) Scenario where NullPointerException occurs

If we have null value in any variable, performing any operation by the variable occurs an NullPointerException.

  1. String s=null;
  2. System.out.println(s.length());//NullPointerException

3) Scenario where NumberFormatException occurs

The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string variable that have characters, converting this variable into digit will occur NumberFormatException.

  1. String s="abc";
  2. int i=Integer.parseInt(s);//NumberFormatException

4) Scenario where ArrayIndexOutOfBoundsException occurs

If you are inserting any value in the wrong index, it would result ArrayIndexOutOfBoundsException as shown below:

  1. int a[]=new int[5];
  2. a[10]=50; //ArrayIndexOutOfBoundsException

Use Five keywords for Handling the Exception

  1. try
  2. catch
  3. finally
  4. throws
  5. throw

Syntax for handling the exception

 try
 {
 /* Block of statements causes problem at run time */
 }
 catch(Type of exception-1 object-1)
 {
 /* Block of statements provides user friendly error message */
}
catch(Type of exception-2 object-2)
 {
 /* Block of statements provides user friendly error message */
 }
 finally
 {
 /* Block of statements which will execute compulsary */

} 

Exception Interview Questions in Java
  • What is an exception in Java?
  • What is the meaning of exception handling?
  • What is the base class for Error and Exception?