public class TestTryCatch {
private boolean method1() throws Exception{
boolean have = false;
try{
String[] str1 = {"123","456"};
System.out.println("" +str1[3]);
have = true;
}catch(Exception e){
System.out.println("In method1  This is exception= "+e);
throw e;
}finally{
have = true;
return have;
}
} private void method2(){
try {
if(method1()){
System.out.println("IN method2 run ");
}
} catch (Exception e) {
System.out.println("In method2 exception="+e);
e.printStackTrace();
}
} public static void main(String[] args) {
TestTryCatch tt = new TestTryCatch();
try {
tt.method2();
} catch (Exception e) {
e.printStackTrace();
}
}}输出:
In method1  This is exception= java.lang.ArrayIndexOutOfBoundsException: 3
IN method2 run 问一下?
为什么 In method2 exception 没有输出来呢?

解决方案 »

  1.   

    根本没有跑到这个异常In method2 exception抛出处。
    System.out.println("IN method2 run ");不会抛出异常的。
      

  2.   

    method2 要 throws 异常
    后面的catch (Exception e) {
    System.out.println("In method2 exception="+e);
    e.printStackTrace();
    加上throw e
      

  3.   

    method1() 中的异常已经被你捕获了,根本就不会抛出异常,所以method2()中不会有异常出现
      

  4.   

    谢谢回贴的朋友原因好像是因为 finally 中有return 而产生的异常丢失!
      

  5.   

    恩  应该是和return有关系吧  里边的e捕获后就消失了  这样的代码好象在哪见过  考试题型好象是
      

  6.   

    小问题!
    public class TestTryCatch {
    private boolean method1() throws Exception {
    boolean have = false;
    try {
    String[] str1 = { "123", "456" };
    System.out.println("" + str1[3]);
    have = true;
    } catch (Exception e) {
    // e.printStackTrace();
    System.out.println("In method1  This is exception= " + e);
    throw e;
    } finally {
    have = true;
    return have;
    }
    } private void method2() {
    try {
    if (method1()) {
    System.out.println("IN method2 run ");
    }
    } catch (Exception t) {
    System.out.println("In method2 exception=" + t);
    t.printStackTrace();
    }
    } public static void main(String[] args) {
    TestTryCatch tt = new TestTryCatch ();
    try {
    tt.method2();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }}