Java Exceptions

The exceptions in java are problematic events which arises during the execution of code. These run-time errors disrupt the normal flow of the program and terminates it abnormally. So it is recommended to handle these run-time errors properly. Exceptions an occur on many different reasons like invalid data provided, file not found, connection to the database lost, server run out of memory etc.

Handling Exceptions

When an exception occur an object is thrown at run-time which need to be handled properly to continue program flow.

Java Exception Types

Java has three type of exceptions.

Checked Exceptions

An exception which occur during the compilation of program is called compile time exception or checked exception. For example ClassNotFoundException, SQLException, NoSuchFieldException, IOException, EOFException etc.

Unchecked Exceptions

An exception which occur during the execution of program is called run-time exception or unchecked exception. For example ArithmeticException, ArrayIndexOutOfBoundsException, NullPointerException, NegativeArraySizeException etc.

Errors

An error is an irrecoverable problem which occur during the execution of program and is beyond the control of programmer or user. For example OutOfMemoryError, VirtualMachineError etc.

Catching Exceptions

In java try and catch keyword are used to catch exceptions. A piece of code which might generate an exception is placed in try block and catch block contains the code which runs if exception occurs. The code which is placed in try and catch blocks is called protected code.

Syntax:

try
{

//Code which might generate exception (protected code)

} catch(ExceptionType ex)
{

//code to be executed if exception occurs

}

Example:

public class JavaExceptionHandling {

public static void main(String args[]){

int r = 0;

try {

r = 6/0;

System.out.println(“End of try block”);

} catch (ArithmeticException ex){

System.out.println(“Java exception: ” + ex);

}

System.out.println(“Out of try catch block”);

}

}

Output:

Java exception: java.lang.ArithmeticException: / by zero
Out of try catch block

Catching Multiple Exceptions

In java multiple catch blocks can be written under try block to catch different exception types.

try
{

//Code which might generate exception (protected code)

} catch(ExceptionType1 ex1)

{

//code to be executed if exception occurs

} catch(ExceptionType2 ex2)

{

//code to be executed if exception occurs

} catch(ExceptionType3 ex3)

{

//code to be executed if exception occurs

}

Example:

public class JavaMultipleCatch{

public static void main(String args[]){

try{

int myList[]=new int[10];

myList[12]=20/4;

}

catch(ArithmeticException e){

System.out.println(“Some arithmetic exception occurred”);

}

catch(ArrayIndexOutOfBoundsException e){

System.out.println(“Some index out of bound exception occurred”);

}

catch(Exception e){

System.out.println(“Some other exception occurred”);

}

System.out.println(“Out of try/catch blocks”);

}

}

Output:

Some index out of bound exception occured
Out of try/catch blocks

 

In latest java versions (SE 7 or above) a single catch block can handle multiple exception types.

catch(SQLException | IOException e) {

//code to be executed if exception occurs

}

The Finally Block

The finally block in java can be optionally written under try block or catch block. The code in finally block will always be executed irrespective of occurrence of an Exception. The finally block can be used to perform cleanup actions like closing BufferedReader, FileInputStream etc.

try
{

//Code which might generate exception (protected code)

} catch(ExceptionType ex)
{

//code to be executed if exception occurs

} finally
{

//Statements will always be executed

}

Leave a Reply

Your email address will not be published. Required fields are marked *