java exception test

test1: 调用者main抛出异常

public class ExceptionTest {

    public void method() throws Exception {
        // 将异常抛出,由调用这个方法的方法去处理这个异常,如果main方法也将异常抛出,则交给Java虚拟机来处理
        System.out.println("Hello World");
        // 抛出异常
        throw new Exception();
    }

    public static void main(String[] args) throws Exception {
        // main方法选择将异常继续抛出
        CartTestt test = new CartTestt();
        test.method(); // main方法需要对异常进行处理
        System.out.println("this line is after exception");
    }

}
output1:

Hello World
Exception in thread “main” java.lang.Exception
at com.ibm.commerce.cw.cart.test.CartTestt.method(CartTestt.java:10)
at com.ibm.commerce.cw.cart.test.ExceptionTest.main(ExceptionTest.java:18)

test2:调用者main try catch 处理异常

public class ExceptionTest {

    public void method() throws Exception {
        // 将异常抛出,由调用这个方法的方法去处理这个异常,如果main方法也将异常抛出,则交给Java虚拟机来处理
        System.out.println("Hello World");
        // 抛出异常
        throw new Exception();
    }

    public static void main(String[] args) {
        // main方法选择将异常继续抛出
        CartTestt test = new CartTestt();
        try {
            test.method();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            System.out.println("this is finally line");
        }
        // main方法需要对异常进行处理
        System.out.println("this line is after exception");
    }

}
output2:

Hello World
java.lang.Exception
at com.ibm.commerce.cw.cart.test.CartTestt.method(CartTestt.java:10)
at com.ibm.commerce.cw.cart.test.ExceptionTest.main(ExceptionTest.java:16)
this is finally line
this line is after exception

可以看出,如果调用者main 不捕获 exception的话,如有exception,则其后的语句不会被执行;如果调用者main 捕获 了exception的话,如有exception,捕获处理之后的语句也会被执行。

test3 try catch + return

public class ExceptionTest {
    public void method() throws Exception {
        // 将异常抛出,由调用这个方法的方法去处理这个异常,如果main方法也将异常抛出,则交给Java虚拟机来处理
        System.out.println("Hello World");
        // 抛出异常
        throw new Exception();
    }

    public static void main(String[] args) {
        // main方法选择将异常继续抛出
        CartTestt test = new CartTestt();
        try {
            test.method();
            return;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            System.out.println("this is finally line");
        }
        // main方法需要对异常进行处理
        System.out.println("this line is after exception");
    }
}
output3

Hello World
java.lang.Exception
at com.ibm.commerce.cw.cart.test.CartTestt.method(CartTestt.java:10)
at com.ibm.commerce.cw.cart.test.ExceptionTest.main(ExceptionTest.java:15)
this is finally line
this line is after exception

test4

public class ExceptionTest {
    public void method() {
        try {
            System.out.println("entering try block");
        } catch (Exception e) {
            System.out.println("entering catch block");
            e.printStackTrace();
        } finally {
            System.out.println("entering finally block");
        }
        System.out.println("this line is after exception");
    }

    public static void main(String[] args) {
        ExceptionTest test = new ExceptionTest();
        test.method();
    }
}
output4

entering try block
entering finally block
this line is after exception

test5 try catch + return

public class ExceptionTest {
    public void method() {
        try {
            System.out.println("entering try block");
            return;
        } catch (Exception e) {
            System.out.println("entering catch block");
            e.printStackTrace();
        } finally {
            System.out.println("entering finally block");
        }
        System.out.println("this line is after exception");
    }

    public static void main(String[] args) {
        ExceptionTest test = new ExceptionTest();
        test.method();
    }
}
output5

entering try block
entering finally block

可以看出,当try block中return时,仍然先执行finally block后再return。

test6 + System.exit(0)

public class ExceptionTest {
    public void method() {
        try {
            System.out.println("entering try block");
            System.exit(0);//终止java虚拟机
        } catch (Exception e) {
            System.out.println("entering catch block");
            e.printStackTrace();
        } finally {
            System.out.println("entering finally block");
        }
        System.out.println("this line is after exception");
    }

    public static void main(String[] args) {
        ExceptionTest test = new ExceptionTest();
        test.method();
    }
}
output6

entering try block

可以看出,当try block中有System.exit(0)时,程序直接return,不再执行finally block。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值