Catching Multiple Exceptions In Java 7
Maybe your like
- Java Exception Handling
- Basic try-catch-finally Exception Handling in Java
- Java Try With Resources
- Catching Multiple Exceptions in Java 7
- Exception Hierarchies
- Checked or Unchecked Exceptions?
- Exception Wrapping
- Fail Safe Exception Handling
- Pluggable Exception Handlers
- Logging Exceptions: Where to Log Exceptions?
- Validation - Throw Exceptions Early
- Validation - Throw Exception or Return False?
- Exception Handling Templates in Java
- Exception Enrichment in Java
- Execution Context
Jakob Jenkov Last update: 2014-06-23 |
In Java 7 it was made possible to catch multiple different exceptions in the same catch block. This is also known as multi catch.
Before Java 7 you would write something like this:
try { // execute code that may throw 1 of the 3 exceptions below. } catch(SQLException e) { logger.log(e); } catch(IOException e) { logger.log(e); } catch(Exception e) { logger.severe(e); }As you can see, the two exceptions SQLException and IOException are handled in the same way, but you still have to write two individual catch blocks for them.
In Java 7 you can catch multiple exceptions using the multi catch syntax:
try { // execute code that may throw 1 of the 3 exceptions below. } catch(SQLException | IOException e) { logger.log(e); } catch(Exception e) { logger.severe(e); }Notice how the two exception class names in the first catch block are separated by the pipe character |. The pipe character between exception class names is how you declare multiple exceptions to be caught by the same catch clause.
Next: Exception Hierarchies| Tweet | |
Jakob Jenkov | |
Copyright Jenkov Aps Close TOC Tag » How To Catch Multiple Exceptions Java
-
Java Multiple Catch Block - GeeksforGeeks
-
Java Catch Multiple Exceptions, Rethrow Exception - DigitalOcean
-
Catching Multiple Exception Types And Rethrowing Exceptions With ...
-
Can I Catch Multiple Java Exceptions In The Same Catch Clause?
-
Java Catch Multiple Exceptions - Programiz
-
Java Multiple Catch Block Example - Javatpoint
-
Java 7 Catch Multiple Exceptions - Javatpoint
-
Java Catch Multiple Exceptions - CodeGym
-
Java 7 Catch Multiple Exceptions - W3schools.blog
-
Catch Multiple Exceptions Before & After Java 7 (examples)
-
How To Catch Multiple Exceptions
-
Catching Multiple Exceptions In Java
-
Catching Multiple Exceptions In Java - More Than One Catch Block
-
Is It Possible To Catch Multiple Java Exceptions In Single Catch Block?