⬅ Previous Topic
Java try-catchNext Topic ⮕
Java try-with-resources⬅ Previous Topic
Java try-catchNext Topic ⮕
Java try-with-resourcesWhen you're writing Java programs, we know that things don’t always go as expected. Files might be missing, input might be wrong, or something might go wrong with math operations like division. That's where exceptions come in — and Java gives you the power to catch them gracefully. But what if more than one type of exception can happen at the same spot in your code? You don’t need to repeat yourself — you can catch multiple exceptions efficiently.
Sometimes, a block of code can throw different exceptions. Instead of having separate try-catch blocks for each one, Java allows you to:
catch
blockstry {
// code that might throw IOException or ArithmeticException
} catch (IOException e) {
System.out.println("IO error occurred");
} catch (ArithmeticException e) {
System.out.println("Arithmetic error occurred");
}
try {
// code that might throw IOException or ArithmeticException
} catch (IOException | ArithmeticException e) {
System.out.println("Exception occurred: " + e.getMessage());
}
import java.io.*;
public class MultiExceptionDemo1 {
public static void main(String[] args) {
try {
int result = 10 / 0; // This will throw ArithmeticException
FileReader fr = new FileReader("nonexistent.txt"); // This would throw FileNotFoundException
} catch (ArithmeticException ae) {
System.out.println("Caught ArithmeticException: " + ae.getMessage());
} catch (FileNotFoundException fnfe) {
System.out.println("Caught FileNotFoundException: " + fnfe.getMessage());
}
}
}
Caught ArithmeticException: / by zero
The division by zero is encountered first, so the ArithmeticException
block is executed. The file reading line is never reached due to the earlier exception.
import java.io.*;
public class MultiExceptionDemo2 {
public static void main(String[] args) {
try {
String str = null;
System.out.println(str.length()); // This will throw NullPointerException
} catch (IOException | NullPointerException e) {
System.out.println("Exception caught: " + e.getClass().getSimpleName());
}
}
}
Exception caught: NullPointerException
Even though IOException
was never thrown here, we include it in the multi-catch to show that multiple exceptions can be handled in a single block — provided they are unrelated.
|
).e
is final
implicitly — you cannot reassign it inside the catch block.If you try to catch two related exceptions (like IOException
and FileNotFoundException
) in a multi-catch, Java will throw a compile-time error.
// ❌ This will not compile
try {
// Some code
} catch (IOException | FileNotFoundException e) {
// Compile-time error: FileNotFoundException is a subclass of IOException
}
In real-world Java applications, you’ll often deal with multiple types of exceptions. Whether you go for multiple catch blocks or the concise multi-catch syntax, the goal is the same: write clean, readable, and robust code. Java gives you the tools — your job is to use them wisely.
try {
int result = 10 / 0;
FileReader fr = new FileReader("file.txt");
} catch (ArithmeticException ae) {
System.out.println("Arithmetic error caught");
} catch (FileNotFoundException fnfe) {
System.out.println("File not found error caught");
}
try {
// some risky operations
} catch (IOException | FileNotFoundException e) {
e.printStackTrace();
}
⬅ Previous Topic
Java try-catchNext Topic ⮕
Java try-with-resourcesYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.