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.   

    因为method1()永远返回的是true 也就是说method2不会发生异常
      

  2.   

    method1中的异常已经在自己的catch中解决了
      

  3.   

    因为你使用了finally{},所以method1()中的异常e永远也不会掷出,method2()也就不会捕捉到这个异常
      

  4.   

    你去掉finally{}一下,异常e肯定会在method2()中被捕捉到的
      

  5.   

    method1()方法甚至可以不声明throws Exception
      

  6.   

    method1()必须要有try{}catch(Exception e){}
    --------------
    否则报错
      

  7.   

    不会的
    最后有finally{
    have = true;
    return have;
    }
    不会抱错的:),你可以试一下
      

  8.   

    分析一下,执行过程就清楚了!
    首先建立TestTryCatch的对象tt,然后调用method2()方法.下面是执行method2().
    一句一句来,先try{}catch语句,在它之中是if语句,if语句调用method1()方法.
    接下来去执行method1()函数,在你的menthod1()中,主体是try{}catch{}finally
    语句,相信你一定知道,finally特性,所以最终method1返回的是true.这也就决定
    在method2()的try不会抛出异常而输出 IN method2 run