I always try to reduce levels of nesting for readability and maintainability. If you have n try/catch blocks, each handling the same type of exception, why not refactor the code that can throw the exception into methods...it would look something like:

try {
    firstBatchOfTricky();
    secondBatchOfTricky();
    ....
    nthBatchOfTricky();
} catch (ItWentBoomException e) {
   // recover from boom
} catch (ItWentBangException e) {
   // recover from bang
}

This is much more readable than having multiple try/catches. Note that your methods should describe what they do in the spirit of self documenting code.

Since you have your own Exception type, you can add the data you need to the exception to do different things in the catch block. When you say 'more specific message', you can just throw the exception with the detailed message; you shouldn't need multiple catch blocks. If you want to do drastically different things based on the state of the exception, just create more exception types and catch blocks but only one try block, as my pseudocode shows...

Finally, if you can't recover from the exception(s), you should not clutter the code with catch blocks. Throw a runtime exception and let it bubble. (Good advice from @tony in the comments)

Answer from hvgotcodes on Stack Overflow
๐ŸŒ
Quora
quora.com โ€บ How-can-one-catch-multiple-exceptions-in-a-single-catch-block-using-Java
How can one catch multiple exceptions in a single catch block using Java? - Quora
Answer: you can do that if : * the catch block catches multiple exceptions using a common base class of Exception * it makes sense to handle these exceptions the same way
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ multiple-exceptions
Java catch Multiple Exceptions
In this tutorial, we will learn to handle multiple exceptions in Java with the help of examples. In Java SE 7 and later, we can now catch more than one type of exception in a single catch block.
Discussions

apex - What is an elegant way to catch multiple exceptions without catching generic exceptions? - Salesforce Stack Exchange
Also, as @Adrian Larson observed, ... want to catch some standard exceptions, or similarly if I have Exceptions which are already extending some other base since Apex doesn't allow multiple inheritance. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... 9 Handing REST API POST Exceptions using SSJS Try-Catch on a ... More on salesforce.stackexchange.com
๐ŸŒ salesforce.stackexchange.com
April 12, 2018
Catching Multiple Exceptions in a Single Catch Block in Java - LambdaTest Community
How can I catch multiple exceptions in the same catch clause in Java? In Java, I want to catch several exceptions at once in a single catch block, like this: try { ... } catch (/* code to catch IllegalArgumentException, SecurityException, IllegalAccessException, and NoSuchFieldException at ... More on community.lambdatest.com
๐ŸŒ community.lambdatest.com
0
March 12, 2025
java - Best practice for handling many exceptions - Software Engineering Stack Exchange
Stack Exchange network consists ... most trusted online community for developers to learn, share their knowledge, and build their careers. Visit Stack Exchange ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I have Java microservice that exposes multiple APIs. Under each API there are many exceptions could be thrown. The last method that catches them all has ... More on softwareengineering.stackexchange.com
๐ŸŒ softwareengineering.stackexchange.com
May 29, 2018
Try Catch issue when trying to catch multiple exceptions

Could you please format your code properly, with all the indentations in place?

More on reddit.com
๐ŸŒ r/learnjava
3
3
August 21, 2020
Top answer
1 of 13
57

I always try to reduce levels of nesting for readability and maintainability. If you have n try/catch blocks, each handling the same type of exception, why not refactor the code that can throw the exception into methods...it would look something like:

try {
    firstBatchOfTricky();
    secondBatchOfTricky();
    ....
    nthBatchOfTricky();
} catch (ItWentBoomException e) {
   // recover from boom
} catch (ItWentBangException e) {
   // recover from bang
}

This is much more readable than having multiple try/catches. Note that your methods should describe what they do in the spirit of self documenting code.

Since you have your own Exception type, you can add the data you need to the exception to do different things in the catch block. When you say 'more specific message', you can just throw the exception with the detailed message; you shouldn't need multiple catch blocks. If you want to do drastically different things based on the state of the exception, just create more exception types and catch blocks but only one try block, as my pseudocode shows...

Finally, if you can't recover from the exception(s), you should not clutter the code with catch blocks. Throw a runtime exception and let it bubble. (Good advice from @tony in the comments)

2 of 13
56

This isn't a performance or personal preferences question: It's a functionality and requirements question.

Suppose I write:

Scenario 1:

try
{
  doThingA();
}
catch (SomeException panic)
{
  System.out.println("doThingA failed");
}
try
{
  doThingB();
}
catch (SomeException panic)
{
  System.out.println("doThingB failed");
}

Scenario 2:

try
{
  doThingA();
  doThingB();
}
catch (SomeException panic)
{
  System.out.println("doThingA or doThingB failed");
}

These two scenarios are not equivalent: They do different things. In Scenario 1, if doThingA throws the exception, doThingB still executes. In Scenario 2, if doThingA throws the exception, doThingB is not executed. So the question is not which gives better performance or which is more readable code, but rather, if doThingA fails, should doThingB still be executed or not?

If what you really want is the second behavior, but you want different messages to tell the user what went wrong, then you should either throw different exceptions, or put the text of the message into the exception, i.e.

void doThingA() throws SomeException
{
  ... whatever code ...
  if (theWorldIsAboutToEnd)
    throw new SomeException("doThingA failed");
}

Then in the catch clause, instead of displaying a constant string, displayt SomeException.toString or SomeException.getMessage.

๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-catch-multiple-exceptions-rethrow-exception
Java Catch Multiple Exceptions, Rethrow Exception | DigitalOcean
August 3, 2022 - Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev ... While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the โ€œreport an issueโ€œ button at the bottom of the tutorial. ... Hi Pankaj, Using single catch block how can I differentiate from where my code is getting exception? try{} catch(IOException ie){ ie.printStackTrace();} catch(SQLException se){ se.printStackTrace();} catch(Exception e){ e.printStackTrace();} now it is replaced with catch(IOException| SQLException |Exception e){ e.printStackTrace();} In the above code my doubt is e.printStackTrace() will print which exception class message whether (IOException / SQLException/Exception )?
๐ŸŒ
Medium
medium.com โ€บ thefreshwrites โ€บ catch-multiple-exceptions-in-java-44cc3ea17ad0
Catch Multiple Exceptions In Java | by Mouad Oumous | The Fresh Writes | Medium
February 17, 2024 - Use Multiple Catch Blocks: Here you can handle multiple exceptions under multiple catch block for a corresponding try block. Program flow will automatically come to the catch block one by one and then, the corresponding catch block is found ...
๐ŸŒ
Rollbar
rollbar.com โ€บ home โ€บ how to catch multiple exceptions in java
3 Ways to Catch Multiple Exceptions in Java (Easy Examples) | Rollbar
August 12, 2024 - Java offers three ways to catch multiple exceptions: using multiple catch blocks for different exception types, the multi-catch feature to handle multiple exceptions in a single block, and a catch-all block for general exception handling.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ java_multi_catch_block.htm
Java Multiple Catch Blocks
The previous statements demonstrate three catch blocks, but you can have any number of them after a single try. If an exception occurs in the protected code, the exception is thrown to the first catch block in the list. If the data type of the exception thrown matches ExceptionType1, it gets ...
Find elsewhere
๐ŸŒ
Centron
centron.de โ€บ startseite โ€บ java catch multiple exceptions, rethrow exception โ€“ tutorial
Java Catch Multiple Exceptions, Rethrow Exception - Tutorial
February 6, 2025 - In Java 7, we can catch both these exceptions in a single catch block as: ... If a catch block handles multiple exceptions, you can separate them using a pipe (|) and in this case, the exception parameter (ex) is final, so you canโ€™t change it.
๐ŸŒ
Java Training School
javatrainingschool.com โ€บ home โ€บ catch multiple exceptions
Catch Multiple Exceptions - Java Training School
April 13, 2023 - ... try { // some code that may throw exceptions } catch (IOException e) { // handle IOException } catch (SQLException e) { // handle SQLException } catch (Exception e) { // handle all other exceptions }
๐ŸŒ
Codefinity
codefinity.com โ€บ courses โ€บ v2 โ€บ 455bd504-41cd-4fd0-98b7-9f3ee575d21a โ€บ 7425cfcb-65fa-4ed7-b6b5-510ed254606b โ€บ 8e86509c-fb4f-4303-b4d1-1a3b75bd6571
Learn Handling Multiple Exceptions | Exceptions
Java allows us to do that by catching both exception objects in a single catch block. ... 123456789101112131415161718192021222324 package com.example; public class Main { public static void main(String[] args) { try { // Call the method that ...
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 1463552 โ€บ how-to-catch-multiple-exceptions-in-a-single-catch-block-in-java
How to catch multiple exceptions in a single catch block in Java? | Sololearn: Learn to code for FREE!
August 20, 2018 - I am using a try catch block and I dont want to use the method to catch Exception to catch all types of exceptions and I want to display the same message for all types of errors. Is there any way to catch multiple exceptions in a single catch box?
๐ŸŒ
LambdaTest Community
community.lambdatest.com โ€บ general discussions
Catching Multiple Exceptions in a Single Catch Block in Java - LambdaTest Community
March 12, 2025 - In Java, I want to catch several ... try { ... } catch (/* code to catch IllegalArgumentException, SecurityException, IllegalAccessException, and NoSuchFieldException at the same time */) { someCode(); } Instead of writing ...
๐ŸŒ
TechVidvan
techvidvan.com โ€บ tutorials โ€บ java-multiple-catch-block
Java Multiple Catch Block - TechVidvan
October 30, 2023 - Starting from Java 7 and onwards, itโ€™s possible to capture multiple exceptions within a single catch block by enclosing them in a single set of parentheses. This is achieved by utilizing the pipe symbol (|) to differentiate between the various types of exceptions you intend to handle ...
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ java-multiple-exception-handling
Multiple Exception Handling in Java (with Codes)
November 28, 2023 - Java 7 introduced the multi-catch feature allowing a single catch block to handle multiple exception types using the pipe (|) separator. This example demonstrates catching both ArrayIndexOutOfBoundsException and NullPointerException in a single ...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 399332 โ€บ java โ€บ multiple-blocks
multiple try blocks (Beginning Java forum at Coderanch)
April 15, 2005 - A try/catch block will catch Exceptions from any statment that throws an Exception. So your code should be modified to... However, let's say that you use methods that might throw an exception: The "catch" will catch any exception thrown by either of the parseInt() calls.
๐ŸŒ
Coding Shuttle
codingshuttle.com โ€บ java-programming-handbook โ€บ multiple-and-nested-try-catch
Multiple and Nested Try-Catch Blocks in Java
April 9, 2025 - Best Practice: Always catch specific exceptions first, then use a generic Exception catch block at the end. A try block inside another try block is called a nested try-catch.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_exceptions_multiple.asp
Java Multiple Exceptions
Java Examples Java Compiler Java ... different errors (exceptions) can happen in the same try block. You can handle them with multiple catch blocks....
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_try_catch.asp
Java Exceptions (Try...Catch)
The 'try catch' is finished. Try it Yourself ยป ยท The throw statement allows you to create a custom error. The throw statement is used together with an exception type. There are many exception types available in Java: ArithmeticException, FileNotFoundException, ArrayIndexOutOfBoundsException, SecurityException, etc: