A try-catch block in Java is used to handle exceptions and prevent the program from terminating unexpectedly.
- The try block contains code that may generate an exception.
- The 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
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
- Java Virtual Machine starts executing the code inside the try block.
- If an exception occurs, the remaining code in the try block is skipped, and the JVM starts looking for the matching catch block.
- If a matching catch block is found, the code in that block is executed.
- After the catch block, control moves to the finally block (if present).
- If no matching catch block is found the exception is passed to the JVM default exception handler.
- The finally block executes in most cases, even if an exception occurs, but it may not execute if the JVM exits abruptly (e.g., via System.exit()), crashes, or there’s an infinite loop before finally.
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 {
// Outer try block
System.out.println("Outer try block started");
// Inner try block 1: handles ArithmeticException
try {
int n = 10;
int res = n / 0; // This will throw ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Caught ArithmeticException in inner try-catch: " + e);
}
// Inner try block 2: handles NullPointerException
try {
String s = null;
System.out.println(s.length()); // This will throw NullPointerException
} catch (NullPointerException e) {
System.out.println("Caught NullPointerException in inner try-catch: " + e);
}
} catch (Exception e) {
// Outer catch block (optional general handler)
System.out.println("Caught exception in outer try-catch: " + e);
} finally {
// Finally block executes in most cases
System.out.println("Finally block executed");
}
System.out.println("Program continues after nested try-catch");
}
}
Output
Caught ArithmeticException: java.lang.ArithmeticException: / by zero
Example: Here, we demonstrate the working of nested try catch block.
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 in inner try-catch: " + e);
}
// Inner try block 2
try {
String s = null;
System.out.println(s.length());
} catch (NullPointerException e) {
System.out.println("Caught NullPointerException in inner try-catch: " + e);
}
} 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 in inner try-catch: java.lang.ArithmeticException: / by zero Caught NullPointerException in inner try-catch: java.lang.NullPointerException: Cannot i...