Exception handling 使用单试块和单次精致处理不同类型的异常

Exception handling 使用单试块和单次精致处理不同类型的异常,exception-handling,Exception Handling,我们有不同类型的数组表达式和intiger表达式异常,一个接一个我需要使用单个try块一次一个地处理这两个异常…请给我答案假设Java代码,您可以尝试以下操作: try{ // do your operation here } catch( Exception e ){ if( e instanceof ArrayExpressionException ){ // handle it } else if( e instanceof IntegerE

我们有不同类型的数组表达式和intiger表达式异常,一个接一个我需要使用单个try块一次一个地处理这两个异常…请给我答案假设Java代码,您可以尝试以下操作:

try{
    // do your operation here
} catch( Exception e ){
    if( e instanceof ArrayExpressionException ){
        // handle it
    }
    else if( e instanceof IntegerExpressionException ){
        // handle it
    }
    else {
        throw e;
    }
}

catch块中声明的异常类型不必是java.lang.exception。但是,它必须是ArrayExpressionException和IntegerExpressionException的共同超类型。例如,它可能是ExpressionException,如果它存在的话。

您应该提供示例代码以帮助其他人帮助您