This has been possible since Java 7. The syntax for a multi-catch block is:
try { ... } catch (IllegalArgumentException | SecurityException | IllegalAccessException | NoSuchFieldException e) { someCode(); }
Remember, though, that if all the exceptions belong to the same class hierarchy, you can simply catch that base exception type.
Also note that you cannot catch both ExceptionA
and ExceptionB
in the same block if ExceptionB
is inherited, either directly or indirectly, from ExceptionA
. The compiler will complain:
Alternatives in a multi-catch statement cannot be related by subclassing Alternative ExceptionB is a subclass of alternative ExceptionA
The fix for this is to only include the ancestor exception in the exception list, as it will also catch exceptions of the descendant type.
Related Posts:
- What is the proper way to handle a NumberFormatException when it is expected?
- EOFException – how to handle?
- unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
- What is a StackOverflowError?
- Exception in thread “main” java.util.NoSuchElementException
- JavaFX – Exception in Application start method?
- JavaFX – Exception in Application start method? [duplicate]
- Given final block not properly padded
- What is an AssertionError? In which case should I throw it from my own code?
- Exception in thread “main” java.util.NoSuchElementException: No line found
- “NoClassDefFoundError: Could not initialize class” error
- Exception in thread “main” java.lang.UnsupportedClassVersionError: a (Unsupported major.minor version 51.0)
- “NoClassDefFoundError: Could not initialize class” error
- “NoClassDefFoundError: Could not initialize class” error
- Why do I get an UnsupportedOperationException when trying to remove an element from a List?
- Why do I get “Exception; must be caught or declared to be thrown” when I try to compile my Java code?
- What is difference between Errors and Exceptions?
- Causes of getting a java.lang.VerifyError
- How to create a custom exception type in Java?
- Exception in thread “main” java.lang.ArithmeticException: / by zero
- Java can’t find file when running through Eclipse
- FXML Load exception
- StringIndexOutOfBoundsException String index out of range: 0
- java.lang.ClassNotFoundException:com.mysql.jdbc.Driver [duplicate]
- Connection Java – MySQL : Public Key Retrieval is not allowed
- Why is a ConcurrentModificationException thrown and how to debug it
- JAVA + try catch(FileNotFoundException e) going in catch(Exception e)?
- Throwing multiple exceptions in Java
- java.lang.ArrayIndexOutOfBoundsException: 0
- Official reasons for “Software caused connection abort: socket write error”
- How can I read a text file in Android?
- Exception is never thrown in body of corresponding try statement
- How can I catch all the exceptions that will be thrown through reading and writing a file?
- Using or ‘|’ in regex [duplicate]
- && (AND) and || (OR) in IF statements
- Unable to find valid certification path to requested target – error even after cert imported
- Java – Convert integer to string [duplicate]
- How does System.out.print() work?
- Initialize part of an array in java
- What causes “Unable to access jarfile” error?
- What is the difference between a static method and a non-static method?
- Is there a method that calculates a factorial in Java?
- What is Java Servlet?
- Generating a Random Number between 1 and 10 Java
- Generating a Random Number between 1 and 10 Java [duplicate]
- How do I generate random integers within a specific range in Java?
- What is the use of printStackTrace() method in Java?
- How do I limit the number of decimals printed for a double?
- Please initialize the log4j system properly warning
- how to iterate in List
- > in java and set their values as we do in a normal int a[i][j] matrix type [duplicate]
- Java – Best way to print 2D array?
- How to update a value, given a key in a hashmap?
- How to schedule a periodic task in Java?
- Recompile with -Xlint:unchecked for detail error in java
- Convert array of strings into a string in Java
- What is Serialization?
- SSL and cert keystore
- What’s the difference between import java.util.*; and import java.util.stream;?
- Collections sort(List
,Comparator super T>) method example - Login Application with 1 stage and multiple scene in JavaFX
- Error: cannot find symbol ArrayList
- Choosing the best concurrency list in Java
- Why nextLine() and not nextString() ?
- Very Basic Java
- What is the meaning of “%d:%02d” in `printf`?
- Type List vs type ArrayList in Java
- Maven error : Could not find or load main class org.codehaus.plexus.classworlds.launcher.Launcher
- Polymorphism vs Overriding vs Overloading
- Difference between Static methods and Instance methods
- How to convert a byte array to a hex string in Java?
- How to get the last value of an ArrayList
- ArithmeticException: “Non-terminating decimal expansion; no exact representable decimal result”
- How to check if the value is integer in java?
- Java Error: Invalid top level statement
- How to make an array of arrays in Java
- Instantiating object of type parameter
- How do I test a class that has private methods, fields or inner classes?
- how to make jni.h be found?
- difference between System.out.println() and System.err.println()
- Selenium — How to wait until page is completely loaded
- Cannot resolve symbol ‘button’ -Android Studio
- Checking to see if array is full
- Tomcat 404 error: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists
- “The import org.springframework cannot be resolved.”
- How to synchronize or lock upon variables in Java?
- Any shortcut to initialize all array elements to zero?
- Simple HTTP server in Java using only Java SE API
- make a JLabel wrap it’s text by setting a max width
- incompatible types: void cannot be converted to int [duplicate]
- Long cannot be dereferenced
- Align printf output in Java
- How to write logs in text file when using java.util.logging.Logger
- Java Memcached Client
- How to enable the Java keyword assert in Eclipse program-wise?
- Map of maps – how to keep the inner maps as maps?
- method does not override or implement a method from a supertype – for Override
- Hibernate show real SQL
- How can I set size of a button?
- Error message ‘Cannot be resolved or is not a field’
- SQL Developer with JDK (64 bit) cannot find JVM