简体   繁体   中英

Java 7 error on multi-catch exception

I run my java application on a MacOS 10.9.5 (Mavericks) with JavaSE 1.7.0_60 installed.

I want to use the multi-catch statement for catch some exceptions...
But When I try to run my app I got an error like this:

Internal Server Error (500) for request GET /tresTest/authenticate
Compilation error (In /app/controllers/TestBoxController.java around line 57)
The file /app/controllers/TestBoxController.java could not be compiled. Error raised is : Syntax error on tokens, delete these tokens

The reason of this error is this line of code:

try {
  class.someMethodTrowingException();
} catch (MyExceptionNo1 | MyExceptionNo2 | MyExceptionNo3 e) {
  doSomethigUseful();
}

If type java -version on my console, i got this result:

java version "1.7.0_60"
Java(TM) SE Runtime Environment (build 1.7.0_60-b19)
Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)

I can't understand why I got this behavior. Multi-catch statement is allow in Java7, right?


UPDATE 10.15.2014

I know that if I use catch in this way everything go right

try {
  class.someMethodTrowingException();
} catch (MyExceptionNo1 e1) {
  doSomethigUseful();
} catch (MyExceptionNo2 e2) {
  doSomethigUseful();
} catch (MyExceptionNo3 e3) {
  doSomethigUseful();
}

But my goal it to use the multi-catch statement like catch (MyExceptionNo1 | MyExceptionNo2 | MyExceptionNo3 e) {...}

Check this out: http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html

UPDATE 11.20.2014

Found out the problem. It's Play that use 1.6 Java compatibility even if I told to its to not use it. Don't understand yet why, and don't understand yet how to sole, but at least I understand where problem is... :)

Add the below plugin in maven to compile it using Java version 1.7

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
    <configuration>
          <source>1.7</source>
          <target>1.7</target>
    </configuration>
</plugin>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM