各位大侠: 
请问:第二个catch为什么捕获不到第一个catch中抛出的Error呢?? package try_catch_finally1_chuli_ErrorInFinally;public class Pool {
static String s = "";
public static void main(String[] args) {
f(0);
System.out.println("------000    1111---------------------");
f(1);
System.out.println("main 0...." + s); } static void f(int i) {
try 
{
if (i == 0) {
s = s + "0";
System.out.println("try if:" + s);
throw new Exception();
}
s = s + "1";
System.out.println("try " + s);
return;

catch (Exception e) {
e.printStackTrace(); s = s + "2";
System.out.println("catch one " + s);
s = s + "3";
System.out.println("catch two" + s);
throw new Error();
}
catch(Error exs){////这里为什么执行不到???
exs.printStackTrace();
System.out.println("catch第二个" + s);
}
finally {
s = s + "4";
System.out.println("finally==" + s);
// throw new RuntimeException();
}
}
}

解决方案 »

  1.   

    Error 是 Throwable 的子类,用于指示合理的应用程序不应该试图捕获的严重问题
    你要是throw new Exception();
    catch(Exception e)这样还可以~~
      

  2.   

    但是
    static void f(int i) {
    try 
    {
    if (i == 0) {
    s = s + "0";
    System.out.println("try if:" + s);
    throw new Error();
    }
    s = s + "1";
    System.out.println("try " + s); return;

    catch (Error e) {
    e.printStackTrace(); s = s + "2";
    System.out.println("catch one " + s);
    s = s + "3";
    System.out.println("catch two" + s);
    //throw new Exception();

    finally {
    s = s + "4";
    System.out.println("finally==" + s);
    // throw new RuntimeException();
    }
    }
    确实可以捕获到Error的,这个又是为什么呢?麻烦你再帮我一下。。呵呵
      

  3.   

    但是 
    static void f(int i) { 
    try 

    if (i == 0) { 
    s = s + "0"; 
    System.out.println("try if:" + s); 
    throw new Error(); 

    s = s + "1"; 
    System.out.println("try " + s); return; 

    catch (Error e) { 
    e.printStackTrace(); s = s + "2"; 
    System.out.println("catch one " + s); 
    s = s + "3"; 
    System.out.println("catch two" + s); 
    //throw new Exception(); 

    finally { 
    s = s + "4"; 
    System.out.println("finally==" + s); 
    // throw new RuntimeException(); 


    确实可以捕获到Error的,这个又是为什么呢?麻烦你再帮我一下。。呵呵
      

  4.   

    一个try可以使用多个catch,以捕获不同类型的异常,但是当前这个try对应的所有的catch都是平级的并且顺序执行的,即第一个catch不符合,会自动寻找下一个,一次递进直至找到匹配的catch或者finally或者代码执行完毕。你代码中的几个catch都是属于这种平级的,由于之前的catch已经是符合条件的catch了,之前catch中重新抛出的Error在之后的catch中不会捕获,而是抛出到上级,如果想捕获的话可以在当前catch的上级,添加一层catch(Error e)
    我的理解
      

  5.   

    catch是相对于try的出口,catch只负责捕获你try中的异常或错误,而且你的问题中,你的error是在catch中抛出的,那么就应该在catch中try并且catch
      

  6.   

    楼上的说的基本上就是那个意思。static void f(int i) {
    try {
    //开始声明try if (i == 0) {
    s = s + "0";
    System.out.println("try if:" + s);
    throw new Exception();
    }
    s = s + "1";
    System.out.println("try " + s);
    return;
    //结束声明try     这个语句块中的Exception 和 Error都可以被捕获 
    //throw new Exception()的话就执行 catch (Exception exs) 中的语句
    //throw new Error()的话就执行 catch (Error exs) 中的语句

    } catch (Exception e) {
    e.printStackTrace(); s = s + "2";
    System.out.println("catch one " + s);
    s = s + "3";
    System.out.println("catch two" + s);

     
    throw new Error();
    //这里的throw new Error() 其实没有被任何try来声明捕获,所以只能交给jvm来处理。注意下面那句catch (Error exs) 其实是最上面的try的声明捕获的  
    } catch (Error exs) {
    exs.printStackTrace();
    System.out.println("catch第二个" + s);
    } finally {
    s = s + "4";
    System.out.println("finally==" + s); }
    }
    其实记住一句话就行了:只有在try{}中抛出的异常或者错误,再能在对应的catch中被捕获并处理。
    这样只要上面的代码稍作修改即可static void f(int i) {
    try {
     if (i == 0) {
    s = s + "0";
    System.out.println("try if:" + s);
    throw new Exception();
    }
    s = s + "1";
    System.out.println("try " + s);
    return;
    } catch (Exception e) {
    e.printStackTrace(); s = s + "2";
    System.out.println("catch one " + s);
    s = s + "3";
    System.out.println("catch two" + s);

    try
    {
    //用try来声明异常的处理 throw new Error();
    }  catch (Error exs) {
    exs.printStackTrace();
    System.out.println("catch第二个" + s);

    } catch (Error exs) {
    exs.printStackTrace();
    System.out.println("catch第二个" + s);
    } finally {
    s = s + "4";
    System.out.println("finally==" + s); }
    }
      

  7.   

    这个理解是对的~~在一楼说的
    Error 是 Throwable 的子类,用于指示合理的应用程序不应该试图捕获的严重问题 。但是你可以捕获,你把程序改成了二楼的那样你就是对它进行捕获了,如果你想把你的一楼的代码中的Error捕获
    你就要这样.
    .
    .
    s = s + "2";
    System.out.println("catch one " + s);
    s = s + "3";
    System.out.println("catch two" + s);
    try {
       throw new Error();   
    } catch (Error e2) {
        System.out.println("将Error再一次抛出");
    }
    .
    .
    .   而如果你不捕获就会交到上一级去处理,而在编译时也不会报异常~~