The try
keyword is used to create a block where there exists a chance of an exception. It is used inside any of the member methods of the class. If an exception occurs at any statement of the try block, an object of the exception class is created and the control moves out of try
block without executing the rest of the statements. Any exception thrown needs to get caught by the catch block. It is recommended to keep only those statements, where there might be a scope of error.
The exception handling block is defined by the catch
keyword. The catch
keyword followed by a specific exception class enclosed within ()
. This block specifies the set of statements that will be executed, whenever the object of that exception class is created and thrown by the try block.
try {
//Fishy Code that might get some exception
} catch(ExceptionClass obj){
//set of statements to be executed when an exception of type ExceptionClass is thrown by try block
}
The above is the simplest block of try-catch. Let us look at an example, which we used in our previous lecture. This time we will use a try-catch block to handle the exception raised.
public class MyException{
public static void main(String args[]){
myFaultyMethod();
}
public static void myFaultyMethod(){
int num=56;
int den=0;
double frac;
try
{
frac=num/den;
System.out.println("Fraction is "+frac);
}
catch(ArithmeticException obj)
{
System.out.println("Arithmetic exception is caught");
}
System.out.println("I will be printed");
}
}
Arithmetic exception is caught
I will be printed
We can have multiple exception types that can arise inside the try block. In order to catch multiple exceptions, we can have multiple catch blocks associated with a single try. It must be noted, that only one exception can arise from the try block. Multiple catch blocks signify the range of exceptions that can be handled.
try{
//Fishy Code that might get some exception
}catch(ExceptionClass obj){
//set of statements to be executed when an exception of type ExceptionClass is thrown by try block
}
catch(ExceptionClass2 obj){
//set of statements to be executed when an exception of type ExceptionClass2 is thrown by try block
}
public class MyException{
public static void main(String args[]){
int num=56;
int den =0;
int myArray[] = new int[5];
double frac;
try
{
myArray[6]=12;
frac=num/den;
System.out.println("Fraction is "+frac);
}
catch(ArrayIndexOutOfBoundsException obj){
System.out.println("Exception:I accessed out of bound in an array");
}
catch(ArithmeticException obj){
System.out.println("Exception:I attempted divide by zero.");
}
System.out.println("I will be printed");
}
}
Exception:I accessed out of bound in an array I will be printed
It is seen that try block can have two types of exceptions i.e ArithmeticException, ArrayIndexOutOfBoundsException. As a result, the exception found first results in the object creation of the same and thrown to be caught. The series of catch blocks are tallied. The matching catch block gets executed. Here we find, that out of bound exception is found earlier to Arithmetic exception. Altering the sequence of the statements inside the try
block will change the output.
public class MyException{
public static void main(String args[]){
int num=56;
int den =0;
int myArray[] = new int[5];
double frac;
try
{
frac=num/den;
System.out.println("Fraction is "+frac);
myArray[6]=12;
}
catch(ArrayIndexOutOfBoundsException obj){
System.out.println("Exception:I accessed out of bound in an array");
}
catch(ArithmeticException obj){
System.out.println("Exception:I attempted divide by zero.");
}
System.out.println("I will be printed");
}
}
Exception:I attempted divide by zero. I will be printed
The ordering of these catch blocks is really very important. The order of the catch blocks should start from child classes and end with the parent class. An attempt to not adhere to it results in a compile-time error.
public class MyException{
public static void main(String args[]){
int num=56;
int den =0;
int myArray[] = new int[5];
double frac;
try
{
myArray[6]=12;
frac=num/den;
System.out.println("Fraction is "+frac);
}
catch(Exception obj){
System.out.println("Exception:Generic Exception");
}
catch(ArrayIndexOutOfBoundsException obj){
System.out.println("Exception:I accessed out of bound in an array");
}
catch(ArithmeticException obj){
System.out.println("Exception:I attempted divide by zero.");
}
System.out.println("I will be printed");
}
}
MyException.java:15: error: exception ArrayIndexOutOfBoundsException has already been caught catch(ArrayIndexOutOfBoundsException obj){ ^ MyException.java:18: error: exception ArithmeticException has already been caught catch(ArithmeticException obj){ ^ 2 errors
Altering the order of catch blocks makes it correct and working. We will be shifting the catch of parent class Exception to the end to handle any exception other than ArithmeticException and ArrayIndexOiutOfBoundsException.
public class MyException{
public static void main(String args[]){
int num=56;
int den =0;
int myArray[] = new int[5];
double frac;
try
{
myArray[6]=12;
frac=num/den;
System.out.println("Fraction is "+frac);
}
catch(ArrayIndexOutOfBoundsException obj){
System.out.println("Exception:I accessed out of bound in an array");
}
catch(ArithmeticException obj){
System.out.println("Exception:I attempted divide by zero.");
}
catch(Exception obj){
System.out.println("Exception:Generic Exception");
}
System.out.println("I will be printed");
}
}
Exception:I accessed out of bound in an array I will be printed
This was all about the try-catch block. In the next lecture, we will introduce throw
and throws
keyword.