Thursday 3 September 2015

Catch multiple Java exceptions in the same catch clause

 Can I catch multiple Java exceptions in the same catch clause


In a java application i need throw same custom exception when some exception arise.So i want to handle multiple exceptions in one catch clause.

for example

    try{

          // write java code here

          } catch( IOException e)
             {
      // write code here
        } catch(  SQLException e){

            // write code here
       }

This is possible since Java 7. The syntax for try-catch block is:


try {
  ...
} catch( IOException | SQLException ex ) {
  ...
}
Prior to Java 7 this was no possible. Remember though, if all the exceptions belong to the same class hierarchy, you can catch that (the base exception type). The only other way is to catch each exception in their own catch block.


Related Posts :

 

 Exception Interrupt Program    
Exception handling example 

Multiple Catch Block Example
 
Nested Try Blocks Example
 
Finally Block Example
 
Without catch block example

The Interface And Class Hierarchy For Collection 

 Java ArrayList Examples

Java Linkedlist Examples

Java ArrayList Examples

Java ArrayList Examples


Java Vector Examples

Java HashSet Examples

Java LinkedHashSet Examples

Java TreeSet Examples

Java Hashtable Examples

Java HashMap Examples

Java TreeMap Examples

 



No comments:

Post a Comment