Java异常淹没、返回值栈

2017-06-14  本文已影响85人  远o_O

返回值栈,异常淹没

/**
 * Created by 编程只服JAVA on 2017.06.14.
 */
public class ExceptionTest {
    public static void main(String[] args) {
        System.out.println(testExc());
    }

    static int testExc(){
        int a = 0;
        try {
            a = a/0;
            return 1;
        }catch (ArithmeticException e){
            throw new IOException("就想抛出来");
        }finally {
            return 2;
        }
    }
}

运行结果:

image.png

运行结果分析:

异常栈追踪,异常处理策略

/**
 * Created by 编程只服JAVA on 2017.06.14.
 */
public class ExceptionTest {
    public static void main(String[] args) throws IOException {
        method1();
    }

    static void method1() throws IOException {
        method2();
    }

    static void method2() throws IOException {
        method3();
    }

    static void method3() throws IOException {
        throw new IOException("method3's Exception");
    }
}

运行结果,注意观察堆栈跟踪:


image.png
上一篇 下一篇

猜你喜欢

热点阅读