try and catch in Java
In java, the trytry and catch, both are the keywords used for exception handling.
The keyword try is used to define a block of code that will be tests the occurence of an exception. The keyword catch is used to define a block of code that handles the exception occured in the respective try block.
The uncaught exceptions are the exceptions that are not caught by the compiler but automatically caught and handled by the Java built-in exception handler.
Both try and catch are used as a pair. Every try block must have one or more catch blocks. We can not use try without atleast one catch, and catch alone can be used (catch without try is not allowed).
The following is the syntax of try and catch blocks.
try{
...
code to be tested
...
}
catch(ExceptionType object){
...
code for handling the exception
...
}
Consider the following example code to illustrate try and catch blocks in Java.
import java.util.Scanner;
public class TryCatchExample {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
System.out.println("Enter the a and b values: ");
try {
int a = read.nextInt();
int b = read.nextInt();
int c = a / b;
System.out.println(a + "/" + b +" = " + c);
}
catch(ArithmeticException ae) {
System.out.println("Problem info: Value of divisor can not be ZERO");
}
}
}
When we run the above code, it produce the following output.
In the above example code, when an exception occurs in the try block the execution control transfered to the catch block and the catch block handles it.
Multiple catch clauses
In java programming language, a try block may has one or more number of catch blocks. That means a single try statement can have multiple catch clauses.
When a try block has more than one catch block, each catch block must contain a different exception type to be handled.
The multipe catch clauses are defined when the try block contains the code that may lead to different type of exceptions.
🔔 The try block generates only one exception at a time, and at a time only one catch block is executed.
🔔 When there are multiple catch blocks, the order of catch blocks must be from the most specific exception handler to most general.
🔔 The catch block with Exception class handler must be defined at the last.
Let's look at the following example Java code to illustrate multiple catch clauses.
public class TryCatchExample {
public static void main(String[] args) {
try {
int list[] = new int[5];
list[2] = 10;
list[4] = 2;
list[10] = list[2] / list[4];
}
catch(ArithmeticException ae) {
System.out.println("Problem info: Value of divisor can not be ZERO.");
}
catch(ArrayIndexOutOfBoundsException aie) {
System.out.println("Problem info: ArrayIndexOutOfBoundsException has occured.");
}
catch(Exception e) {
System.out.println("Problem info: Unknown exception has occured.");
}
}
}
When we run the above code, it produce the following output.
Nested try statements
The java allows to write a try statement inside another try statement. A try block within another try block is known as nested try block.
When there are nested try blocks, each try block must have one or more seperate catch blocks.
Let's look at the following example Java code to illustrate nested try statements.
public class TryCatchExample {
public static void main(String[] args) {
try {
int list[] = new int[5];
list[2] = 10;
list[4] = 2;
list[0] = list[2] / list[4];
try {
list[10] = 100;
}
catch(ArrayIndexOutOfBoundsException aie) {
System.out.println("Problem info: ArrayIndexOutOfBoundsException has occured.");
}
}
catch(ArithmeticException ae) {
System.out.println("Problem info: Value of divisor can not be ZERO.");
}
catch(Exception e) {
System.out.println("Problem info: Unknown exception has occured.");
}
}
}
When we run the above code, it produce the following output.
🔔 In case of nested try blocks, if an exception occured in the inner try block and it's catch blocks are unable to handle it then it transfers the control to the outer try's catch block to handle it.