Try Catch in Java – Exception handling
Try block
The try block contains a set of statements where an exception can occur. A try block is always followed by a catch block, which handles the exception that occurs in an associated try block. A try block must be followed by catch blocks or finally block or both.
Catch block
A catch block is where you handle the exceptions, this block must follow the try block. A single try block can have several catch blocks associated with it. You can catch different exceptions in different catch blocks. When an exception occurs in a try block, the corresponding catch block that handles that particular exception executes. For example, if an arithmetic exception occurs in the try block then the statements enclosed in the catch block for arithmetic exception executes.
Syntax of try catch in java:
try { //statements that may cause an exception } catch (exception(type) e(object)) { //error handling code }
Example of Try-catch Block
public class Exception Handling{ public static void main(String[] args) { try { System.out.println("hello" + " " + 1/0); //System.out.println(1/0); } catch (ArithmeticException e) { System.out.println("world"); } } }
Output:
World
In the example, we are trying to divide a number by 0
. Here, this code generates an exception. To handle the exception, we have put the code, 1 / 0
inside the Try block. Now when an exception occurs, the rest of the code inside the Try block is skipped. The Catch block catches the exception and statements inside the catch block are executed. If none of the statements in the Try block generates an exception, the catch block is skipped.
Example of Multiple Catch Blocks:
After a try block, it is possible to include one or more catch blocks. Each catch block should include a unique exception handler. When there are multiple exceptions that require different actions to be taken, the Java multi-catch block should be used.
Points to remember
- At a time only one exception occurs and at a time only one catch block is executed.
- All catch blocks must be ordered from most specific to most general, i.e. catch for ArithmeticException must come before catch for Exception.
Example of Multiple catch block:
public class MultipleCatchBlock{ public static void main(String[] args) { try { int a[] = new int[10]; a[10] = 50 / 0; } catch (ArithmeticException e) { System.out.println("Arithmetic Exception "); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBounds Exception "); } catch (Exception e) { System.out.println("Parent Exception "); } System.out.println("Execution done"); }
Output:
Arithmetic Exception
Execution Done
Example of Multiple Catch BlocKs
public class MultipleCatchBlock{
public static void main(String[] args) {
try {
int a[] = new int[10];
System.out.println(a[20]);
a[5] = 50 / 0;
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception ");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBounds Exception ");
} catch (Exception e) {
System.out.println("Parent Exception ");
}
System.out.println("Execution done");
}
Output:
Arithmetic Exception
Execution Done
Another Example of Multiple Catch Blocks
public class MuLtipleCatchBlock{
public static void main(String[] args) {
try {
String s = null;
System.out.println(s.length());
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception ");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBounds Exception ");
} catch (Exception e) {
System.out.println("Parent Exception ");
}
System.out.println("Execution done");
}
}
Output:
Arithmetic Exception
Execution Done
Example of Catch Block without maintaining Order
Let’s see an example, to handle the exception without maintaining the order of exceptions.
public class MultipleCatchBlock{
public static void main(String[] args) {
try {
int a[] = new int[10];
System.out.println(a[20]);
a[5] = 50 / 0;
catch (Exception e) {
System.out.println("Parent Exception ");
}
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception ");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBounds Exception ");
}
System.out.println("Execution done");
}
Output:
Compile-time error
Arithmetic Exception
Execution Done
Arithmetic Exception
Execution Done
Another Example of Multiple Catch Blocks
public class MuLtipleCatchBlock{ public static void main(String[] args) { try { String s = null; System.out.println(s.length()); } catch (ArithmeticException e) { System.out.println("Arithmetic Exception "); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBounds Exception "); } catch (Exception e) { System.out.println("Parent Exception "); } System.out.println("Execution done"); } }
Output:
Arithmetic Exception
Execution Done
Example of Catch Block without maintaining Order
Let’s see an example, to handle the exception without maintaining the order of exceptions.
public class MultipleCatchBlock{
public static void main(String[] args) {
try {
int a[] = new int[10];
System.out.println(a[20]);
a[5] = 50 / 0;
catch (Exception e) {
System.out.println("Parent Exception ");
}
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception ");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBounds Exception ");
}
System.out.println("Execution done");
}
Output:
Compile-time error
Arithmetic Exception
Execution Done
Let’s see an example, to handle the exception without maintaining the order of exceptions.
public class MultipleCatchBlock{ public static void main(String[] args) { try { int a[] = new int[10]; System.out.println(a[20]); a[5] = 50 / 0; catch (Exception e) { System.out.println("Parent Exception "); } } catch (ArithmeticException e) { System.out.println("Arithmetic Exception "); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBounds Exception "); } System.out.println("Execution done"); }
Output:
Compile-time error
Compile-time error