Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
Starting from Java 7, you can catch multiple exceptions in a single catch block using the multi-catch feature. This can help you reduce code duplication and make your exception handling code more concise. This tutorial will cover the basics of catching multiple exceptions in Java, including some examples and best practices.
To catch multiple exceptions in a single catch block, use the pipe character (|
) to separate the exception types in the catch statement.
Example:
public class Main { public static void main(String[] args) { try { // Code that may throw exceptions throwIOExceptionOrNumberFormatException(); } catch (IOException | NumberFormatException e) { System.err.println("Caught an IOException or NumberFormatException: " + e.getMessage()); } } public static void throwIOExceptionOrNumberFormatException() throws IOException, NumberFormatException { // This method is just for demonstration purposes if (Math.random() < 0.5) { throw new IOException("Sample IOException"); } else { throw new NumberFormatException("Sample NumberFormatException"); } } }
In this example, the try
block contains code that may throw IOException
or NumberFormatException
. The catch
block catches either exception and handles them in the same way.
e
in the example) is effectively final
inside the multi-catch block. This means you cannot reassign the variable within the block. If you need to rethrow the exception or modify it, you will need to use separate catch blocks.This tutorial introduced the basics of catching multiple exceptions in Java using the multi-catch feature, including some examples and best practices. Catching multiple exceptions in a single catch block can help you reduce code duplication and make your exception handling code more concise and organized.
Handling multiple exceptions in Java:
catch
block or use separate catch
blocks for each exception type.try { // Code that may throw exceptions } catch (ExceptionType1 | ExceptionType2 e) { // Handling multiple exceptions }
Java catch multiple exceptions in one block:
catch
block using the multi-catch feature introduced in Java 7.try { // Code that may throw exceptions } catch (ExceptionType1 | ExceptionType2 e) { // Handling multiple exceptions }
Try-catch block with multiple exceptions in Java:
try
block to enclose code that may throw multiple exceptions, and handling them in a corresponding catch
block.try { // Code that may throw exceptions } catch (ExceptionType1 e1) { // Handling ExceptionType1 } catch (ExceptionType2 e2) { // Handling ExceptionType2 }
Java catch block order with multiple exceptions:
catch
blocks is crucial. If you have a catch block for a more general exception before a more specific one, it won't compile.try { // Code that may throw exceptions } catch (ExceptionType1 e1) { // Handling ExceptionType1 } catch (Exception e) { // This catch block won't be reached for ExceptionType1 }
Java try-catch-finally with multiple exceptions:
finally
block to execute code regardless of whether an exception is thrown or not.try { // Code that may throw exceptions } catch (ExceptionType1 e1) { // Handling ExceptionType1 } catch (ExceptionType2 e2) { // Handling ExceptionType2 } finally { // Cleanup code }
Handling checked and unchecked exceptions together in Java:
catch
block or use separate catch
blocks for each.try { // Code that may throw exceptions } catch (CheckedException e) { // Handling checked exception } catch (UncheckedException e) { // Handling unchecked exception }
Java multi-catch block vs separate catch blocks:
try { // Code that may throw exceptions } catch (ExceptionType1 | ExceptionType2 e) { // Handling multiple exceptions in one block }
Java try-catch resources with multiple exceptions:
try (Resource1 res1 = new Resource1(); Resource2 res2 = new Resource2()) { // Code that may throw exceptions } catch (ExceptionType1 | ExceptionType2 e) { // Handling multiple exceptions }
Nested try-catch blocks for multiple exceptions in Java:
try-catch
blocks to handle different exceptions at different levels of code.try { // Outer try block try { // Inner try block } catch (ExceptionType1 e1) { // Handling ExceptionType1 } } catch (ExceptionType2 e2) { // Handling ExceptionType2 }
Java 7 and later features for handling multiple exceptions:
catch
block, improving code readability.try { // Code that may throw exceptions } catch (ExceptionType1 | ExceptionType2 e) { // Handling multiple exceptions }