看看这个测试,是否对你们有帮助 class MyException extends Exception { public MyException(String msg){ super(msg); } } public class ExceptionTest { public ExceptionTest() { } public void f() throws Exception{ try{ throw new MyException("MyException in f()"); }finally{ System.out.println("Finally in f()"); } } public void g() throws Exception{ try{ f(); }catch(Exception e){ e.printStackTrace(); System.out.println("Cath Exception in g()"); throw new MyException("MyException in g()"); } } public static void main(String[] args) { ExceptionTest t = new ExceptionTest(); try{ t.g(); }catch(Exception e){ e.printStackTrace(); } }} 输出: Finally in f() untitled1.MyException: MyException in f() at untitled1.ExceptionTest.f(ExceptionTest.java:18) at untitled1.ExceptionTest.g(ExceptionTest.java:26) at untitled1.ExceptionTest.main(ExceptionTest.java:38) at java.lang.reflect.Method.invoke(Native Method) at com.borland.jbuilder.util.BootStrap.invokeMain(Unknown Source) at com.borland.jbuilder.util.BootStrap.main(Unknown Source) Cath Exception in g() untitled1.MyException: MyException in g() at untitled1.ExceptionTest.g(ExceptionTest.java:30) at untitled1.ExceptionTest.main(ExceptionTest.java:38) at java.lang.reflect.Method.invoke(Native Method) at com.borland.jbuilder.util.BootStrap.invokeMain(Unknown Source) at com.borland.jbuilder.util.BootStrap.main(Unknown Source)
我想是做不到!
可以可以,佩服,佩服版主!
这样的话已经违背了try/catch的原意了。
倒不是一定要说出对错,其实我也从没有这么做过也没有碰到过这样的问题,只是看到这个问题,提出想法而已。具体能否实行也在实验,我觉得既然有这样的想法就可以做这样的实验。另外,希望hiseh()能说出这样做的原因。应该不会是凭空想象出来的吧。
我想有时违背常理的问题也还是蛮有启发性的。
以后可能会有,赫赫。
class MyException extends Exception {
public MyException(String msg){
super(msg);
}
}
public class ExceptionTest
{ public ExceptionTest()
{
} public void f() throws Exception{
try{
throw new MyException("MyException in f()");
}finally{
System.out.println("Finally in f()");
}
} public void g() throws Exception{
try{
f();
}catch(Exception e){
e.printStackTrace();
System.out.println("Cath Exception in g()");
throw new MyException("MyException in g()");
} }
public static void main(String[] args)
{
ExceptionTest t = new ExceptionTest();
try{
t.g();
}catch(Exception e){
e.printStackTrace();
}
}}
输出:
Finally in f()
untitled1.MyException: MyException in f()
at untitled1.ExceptionTest.f(ExceptionTest.java:18)
at untitled1.ExceptionTest.g(ExceptionTest.java:26)
at untitled1.ExceptionTest.main(ExceptionTest.java:38)
at java.lang.reflect.Method.invoke(Native Method)
at com.borland.jbuilder.util.BootStrap.invokeMain(Unknown Source)
at com.borland.jbuilder.util.BootStrap.main(Unknown Source)
Cath Exception in g()
untitled1.MyException: MyException in g()
at untitled1.ExceptionTest.g(ExceptionTest.java:30)
at untitled1.ExceptionTest.main(ExceptionTest.java:38)
at java.lang.reflect.Method.invoke(Native Method)
at com.borland.jbuilder.util.BootStrap.invokeMain(Unknown Source)
at com.borland.jbuilder.util.BootStrap.main(Unknown Source)