Last Updated : 03 Jun, 2025
A try-catch block in Java is a mechanism to handle exceptions. This make sure that the application continues to run even if an error occurs. The code inside the try block is executed, and if any exception occurs, it is then caught by the catch block.
Example: Here, we are going to handle the ArithmeticException using a simple try-catch block.
Java
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");
}
}
Exception caught: java.lang.ArithmeticException: / by zero I will always executeSyntax of try Catch Block
1. try in Javatry {
// Code that might throw an exception
} catch (ExceptionType e) {
// Code that handles the exception
}
The try block contains a set of statements where an exception can occur.
2. catch in Javatry
{
// statement(s) that might cause exception
}
The catch block is used to handle the uncertain condition of a try block. A try block is always followed by a catch block, which handles the exception that occurs in the associated try block.
Internal working of try-catch Blockcatch
{
// statement(s) that handle an exception
// examples, closing a connection, closing
// file, exiting the process after writing
// details to a log file.
}
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.
Java
import java.util.*;
class Geeks {
public static void main(String[] args) {
try {
// ArithmeticException
int res = 10 / 0;
// NullPointerException
String s = null;
System.out.println(s.length());
}
catch (ArithmeticException e) {
System.out.println(
"Caught ArithmeticException: " + e);
}
catch (NullPointerException e) {
System.out.println(
"Caught NullPointerException: " + e);
}
}
}
Caught ArithmeticException: java.lang.ArithmeticException: / by zero
Example: Here, we demonstrate the working of nested try catch block.
Java
import java.util.*;
public class Geeks {
public static void main(String[] args) {
try {
// Outer try block
System.out.println("Outer try block started");
try {
// Inner try block 1
int n = 10;
int res = n / 0;
} catch (ArithmeticException e) {
System.out.println
("Caught ArithmeticException in inner try-catch: " + e);
}
try {
// Inner try block 2
String s = null;
System.out.println(s.length());
} catch (NullPointerException e) {
System.out.println
("Caught NullPointerException in inner try-catch: " + e);
}
} catch (Exception e) {
// Outer catch block
System.out.println
("Caught exception in outer try-catch: " + e);
} finally {
// Finally block
System.out.println("Finally block executed");
}
}
}
Output:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4