当前位置: 代码迷 >> J2SE >> 哪位大神有时间看一下,错误处理机制,想不通为什么这么个走法
  详细解决方案

哪位大神有时间看一下,错误处理机制,想不通为什么这么个走法

热度:37   发布时间:2016-04-23 20:27:54.0
哪位大神有时间看一下,异常处理机制,想不通为什么这么个走法?
public class Demo4Exception {  
    public Demo4Exception() {  
    }  

    boolean method1() throws Exception {  
        boolean flag = true;  
        try {  
            flag = method2();  
        } catch (Exception e) {  
            System.out.println("method1, catch exception");  
            flag = false;  
            throw e;  
        } finally {  
            System.out.println("method1, finally; return flag = " + flag);  
            return flag;  
        }  
    }  
  
    boolean method2() throws Exception {  
        boolean flag = true;  
        try {  
            flag = method3(); 
            if (!flag) {  
                return false;  
            }  
            System.out.println("method2, at the end of try");  
            return flag;  
        } catch (Exception e) {  
            System.out.println("method2, catch exception");  
            flag = false;  
            throw e;  
        } finally {  
            System.out.println("method2, finally; return flag = " + flag);  
            return flag;  
        }  
    }  
  
    boolean method3() throws Exception {  
        boolean flag = true;  
        try {  
            int b = 12;  
            int c;  
            for (int i = 2; i >= -2; i--) {  
                c = b / i;  
                System.out.println("i=" + i);  
            }  
            return true;  
        } catch (Exception e) {  
            System.out.println("method3, catch exception");  
            flag = false;  
            throw e;  
        } finally {  
            System.out.println("method3, finally; return flag = " + flag);  
            return flag;  
        }  
    }  
  
    public static void main(String[] args) {  
        Demo4Exception d4e = new Demo4Exception();  
        try {  
            System.out.println(d4e.method1());