关于JAVA异常处理的问题
?创建一个异常类MyException。
?创建一个运程程序类,该类包含多个方法Method A、Method B、Method C。这些方法形成三级调用,主方法main调用Method A,Method A调用Method B,Method B调用Method C,最后Method C会抛出一个MyException的实例对象。
?Method A会捕获到Method C抛出的MyException对象两次。
?main方法也会捕获到Method C抛出的MyException对象两次。
?其运行结果如下所示:
Method A called!
Method B called!
Method C called!
Exception throw in MyException!
Exception caught in Method A
Exception finally statement in Method A
Exception caught in Main
Exception finally statement in Main
以下是我的代码:
public class MyException { public static void main (String[] args) { new MyException().MethodA(); } public void MethodA(){ System.out.println("Method A called!"); try{ this.MethodB(); }catch(NewException e){ System.err.println(e); System.out.println("Exception caught in Method A"); }finally{ System.out.println("Exception finally statement in Method A"); } } public void MethodB()throws NewException{ System.out.println("Method B called!"); this.MethodC(); } public void MethodC()throws NewException{ System.out.println("Method C called!"); throw new NewException(); } }class NewException extends Exception{ public String toString(){ return ("Exception throw in MyException!"); }}public class MyException { public static void main (String[] args) { new MyException().MethodA(); } public void MethodA()[color=#FF0000]throws NewException[/color]{ System.out.println("Method A called!"); try{ this.MethodB(); }catch(NewException e){ System.err.println(e); System.out.println("Exception caught in Method A"); [color=#FF0000]throw e[/color] }finally{ System.out.println("Exception finally statement in Method A"); } } public void MethodB()throws NewException{ System.out.println("Method B called!"); this.MethodC(); } public void MethodC()throws NewException{ System.out.println("Method C called!"); throw new NewException(); } }class NewException extends Exception{ public String toString(){ return ("Exception throw in MyException!"); }}
[解决办法]
public void MethodA()[color=#FF0000]throws NewException[/color]{ System.out.println("Method A called!"); try{ this.MethodB(); }catch(NewException e){ System.err.println(e); System.out.println("Exception caught in Method A"); [color=#FF0000]throw e[/color] }finally{ System.out.println("Exception finally statement in Method A"); }这样抛出,在main方法也捕获不到吧 ????????
[解决办法]
public class Test{
public static void main(String[] args){
Test test = new Test();
try{
test.MethodA();
}catch(MyException e){
System.out.println(" Exception caught in Main");
}finally{
System.out.println(" Exception finally statement in Main");
}
}
private void MethodA() throws MyException{
System.out.println("Method A called!");
try{
this.MethodB();
}catch(MyException e){
System.out.println(" Exception caught in Method A");
throw new MyException();
}finally{
System.out.println(" Exception finally statement in Method A");
}
}
private void MethodB() throws MyException{
System.out.println("Method B called!");
this.MethodC();
}
private void MethodC() throws MyException{
System.out.println("Method C called!");
throw new MyException();
}
}