A try-catch block in Java is used to handle exceptions and prevent the program from terminating unexpectedly.
- Try block contains code that may generate an exception.
- Catch block handles the exception if it occurs.
Example: Handling the ArithmeticException using a simple try-catch block.
import java.io.*;
class Geeks {
public static void main(String[] args) {
try {
// This will throw an ArithmeticException
int res = 10 / 0;
}
// Here we are Handling the exception
catch (ArithmeticException e) {
System.out.println("Exception caught: " + e);
}
// This line will executes weather an exception
// occurs or not
System.out.println("I will always execute");
}
}
Output
Exception caught: java.lang.ArithmeticException: / by zero I will always execute
Syntax of try Catch Block
The basic syntax of a try-catch block in Java is:
try { // Code that might throw an exception}catch (ExceptionType e) {// Code that handles the exception}
1. try Block
The try block contains a set of statements where an exception can occur.
try
{
// statement(s) that might cause exception
}
2. catch Block
The catch block is used to handle the uncertain condition of a try block. A try block must be followed by at least one catch block or a finally block. which handles the exception that occurs in the associated try block.
catch
{
// statement(s) that handle an exception
// examples, closing a connection, closing
// file, exiting the process after writing
// details to a log file.
}
Internal Working of try-catch Block
When Java executes a try-catch block, the following steps occur:
- The code inside the try block is executed.
- If an exception occurs, the remaining statements in the try block are skipped.
- Java searches for a matching catch block to handle the exception.
- If a matching catch block is found, it is executed and the program continues after the exception-handling construct.
- If no suitable handler is found, the exception propagates up the call stack and may eventually cause the program to terminate.
try{
// this will throw ArithmeticException
int ans = 10/0;} catch(ArithmeticException e) {
System.out.println("caught ArithmeticException");
} finally {
System.out.println("I will always execute whether an Exception occur or not");
}
Example: Here, we demonstrate the working try catch block with multiple catch statements.
import java.util.*;
public class Geeks {
public static void main(String[] args)
{
try {
System.out.println("Outer try block started");
// Inner try block 1
try {
int n = 10;
int res = n / 0;
}
catch (ArithmeticException e) {
System.out.println(
"Caught ArithmeticException: "
+ e.getClass().getName());
}
// Inner try block 2
try {
String s = null;
System.out.println(s.length());
}
catch (NullPointerException e) {
System.out.println(
"Caught NullPointerException: "
+ e.getClass().getName());
}
}
catch (Exception e) {
System.out.println(
"Caught exception in outer try-catch: "
+ e);
}
finally {
System.out.println("Finally block executed");
}
System.out.println(
"Program continues after nested try-catch");
}
}
Output
Outer try block started Caught ArithmeticException: java.lang.ArithmeticException Caught NullPointerException: java.lang.NullPointerException Finally block executed Program continues after nested try-...