异常之间的管道

时间:2012-02-01 19:29:16

标签: java exception try-catch

当编译错误提示我添加异常时,我在eclipse中编写代码。我点击了对现有catch语句的添加异常,并创建了一个如下所示的catch块:

try{

}catch(SQLException | InvalidKeyException | BadPaddingException
            | IllegalBlockSizeException | NoSuchAlgorithmException
            | NoSuchPaddingException e){

        e.printStackTrace();
}

我之前从未见过这种语法,但它通过eclipse运行良好。当我通过AIX服务器运行它时出现了这些错误:

forecast_transmission_v2.java:813: <identifier> expected
            }catch(SQLException | InvalidKeyException
                               ^
forecast_transmission_v2.java:813: '{' expected
            }catch(SQLException | InvalidKeyException
                                 ^
forecast_transmission_v2.java:814: illegal start of expression
                            | BadPaddingException | IllegalBlockSizeException
                            ^
forecast_transmission_v2.java:814: ';' expected
                            | BadPaddingException | IllegalBlockSizeException
                                                 ^
forecast_transmission_v2.java:815: not a statement
                            | NoSuchAlgorithmException | NoSuchPaddingException e){
                                                       ^
forecast_transmission_v2.java:815: ';' expected
                            | NoSuchAlgorithmException | NoSuchPaddingException e){
                                                                               ^
forecast_transmission_v2.java:815: not a statement
                            | NoSuchAlgorithmException | NoSuchPaddingException e){
                                                                                ^
forecast_transmission_v2.java:815: ';' expected
                            | NoSuchAlgorithmException | NoSuchPaddingException e){

我假设这种语法对Java7来说是新的,这就是为什么会发生这种情况。任何人都可以验证这个或是否有其他原因导致此错误?

2 个答案:

答案 0 :(得分:6)

确实是Java 7 feature

答案 1 :(得分:1)

是的,这种语法是Java 7的新增功能。请参阅http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html