Fangen mehrerer Exceptions in einem Block

Java 7 and later

Multiple-exception catches are supported, starting in Java 7.

The syntax is:

try { 
  // stuff 
} catch (Exception1 | Exception2 ex) { 
  // Handle both exceptions 
}

The static type of ex is the most specialized common supertype of the exceptions listed. There is a nice feature where if you rethrow ex in the catch, the compiler knows that only one of the listed exceptions can be thrown.

 

Java Version in Maven setzen

Statt das Maven Plugin zu konfigurieren ist es auch möglich einfach die Java-Properties in der pom.xml zu setzen.

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>