如何使用mult-catch子句?

时间:2013-08-16 07:39:46

标签: java exception try-catch

class Demo{
public static void main(String args[]){
    int x=3,n=5,d=0;
    int ar[]=new int[3];
    String name="Neno";

    System.out.println("Start main");
    try{
        ar[x]=name.charAt(n)/d; //n=5
    }catch(StringIndexOutOfBoundsException e){
        System.out.println("String index Error");
    }catch(RuntimeException e){
        System.out.println("Any runtime Error");
    }catch(ArrayIndexOutOfBoundsException e){
        System.out.println("Array index Error");
    }catch(ArithmeticException e){
        System.out.println("Arithmetic Error");
    }

    System.out.println("End   main");
}
}

我使用此代码来过滤一些异常,但代码中存在错误。它说要删除ArrayIndexOutOfBoundsArithmeticException的catch子句。是因为错误弹出的catch-clauses的顺序?当我改变这样的顺序......

class Demo{
public static void main(String args[]){
    int x=3,n=5,d=0;
    int ar[]=new int[3];
    String name="Niroth";

    System.out.println("Start main");
    try{
        ar[x]=name.charAt(n)/d; //n=5
    }catch(StringIndexOutOfBoundsException e){
        System.out.println("String index Error");
    }catch(ArrayIndexOutOfBoundsException e){
        System.out.println("Array index Error");
    }catch(ArithmeticException e){
        System.out.println("Arithmetic Error");
    }catch(RuntimeException e){
        System.out.println("Any runtime Error");
    }catch(Exception e){
        System.out.println("Any Error");
    }

    System.out.println("End   main");
}
}

此顺序没有错误。谁能解释一下这个问题的原因?

7 个答案:

答案 0 :(得分:5)

捕捉异常就像是一个接一个地放置的桶。你必须在最后一个中找到更广泛的。希望以下有所帮助:

错:

 \                           /
  \_____RuntimeException____/

      \                  /
       \__AIOBException_/        //ArrayIndexOutOfBounds

      \                 /
       \__AriException_/        //ArithmeticException

正确:

      \                  /
       \__AIOBException_/        //ArrayIndexOutOfBounds

      \                 /
       \__AriException_/        //ArithmeticException 

 \                           /
  \_____RuntimeException____/

答案 1 :(得分:4)

您必须在超类之前捕获特定的例外(更广泛的例外,即RuntimeExceptionException

请在此处查看相同的问题/答案:Order of catching exceptions in Java

答案 2 :(得分:1)

catch条款的顺序

这是我的合理解释。

当你有

catch(Exception e){
   System.out.println("Any Error");
}
catch(RuntimeException e){
   System.out.println("Any runtime Error");
}

你可以想象,每发生一次异常,它都会被Exception子句捕获,永远不会到达/使用另一个块RuntimeException

答案 3 :(得分:1)

ArithmeticExceptionArrayIndexOutOfBoundsExceptionStringIndexOutOfBoundsException都延伸RuntimeException(最后两个来自IndexOutOfBoundsException) - 所以在您的第一个代码段中,{{1永远不会达到捕捉。

您应该始终首先捕获最具体的异常,然后使用最不具体的异常完成,就像在第二个代码段中一样。

干杯,

答案 4 :(得分:1)

是的,订单很重要。将执行匹配异常的第一个catch子句,其余的将被忽略。

作为旁注,通常你应该抓住RuntimeExceptionRuntimeException很可能意味着您的逻辑错误,或者您没有进行正确的检查。

答案 5 :(得分:0)

您需要订购从最具体到最不具体的例外。

在您的第一个示例中,RuntimeException位于catchArrayIndexOutOfBoundsException之前ArithmeticException块中间的某个位置,它们都是RuntimeException个。

这意味着永远不会达到ArrayIndexOutOfBoundsExceptionArithmeticException的catch块,因为它们会扩展RuntimeException,其catch块将处理异常。

通过排序从最后一个特定到最具体的异常(根据类层次结构),您可以为解决异常提供每个异常。

答案 6 :(得分:0)

应该从更具体到普遍的特征中捕获异常。换句话说,在超类之前捕获子类的异常。

try
{
int e=33/0;
}

catch(ArithmeticException e)
{
//code here
}

catch (Exception e)
{
}