Diogo Oliveira de Melo

multi-catch statement is not supported in -source 1.6

On a project I’m working on, I tried to use a multi-catch statement, something like this:

try {
    something()
} catch (AException | BException | CException | DException e) {
    handleMyException(e);
}

When building with Maven mvn clean install, it gave the following error:

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/dmelo/proj2/File.java:[10,38] multi-catch statement is not supported in -source 1.6
  (use -source 7 or higher to enable multi-catch statement)
[INFO] 1 error
[INFO] -------------------------------------------------------------

After seeing this, my question was: how to specify the compatibility version to use? The answer is to ADD a new plugin to the pom.xml like this:

<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
...
    <build>
        <plugins>
            <!--
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            ...
        </plugins>
        ...
    </build>
    ...
</project>

After inserting that new section, mvn clean install went smoothly :).