各位大侠:
请问:第二个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();
}
}
}
请问:第二个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();
}
}
}
你要是throw new Exception();
catch(Exception e)这样还可以~~
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的,这个又是为什么呢?麻烦你再帮我一下。。呵呵
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的,这个又是为什么呢?麻烦你再帮我一下。。呵呵
我的理解
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); }
}
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再一次抛出");
}
.
.
. 而如果你不捕获就会交到上一级去处理,而在编译时也不会报异常~~