java重新抛出异常_java 重新抛出异常

一.有时希望把刚捕获的异常重新抛出,尤其时在使用Exception捕获所以异常的时候,既然已经得到了对当前异常对象的引用,可以重新把它抛出:

catch(Exception e){

System.out.println("An exception was thrown");throwe;

}

二 :

1.重新抛出异常会把异常抛给上一级环境中的异常处理程序,同一个try块的后续catch字句将忽略.

2.异常对象的所有信息都得以保持,所以高一级环境中捕获此异常的处理程序可以从这个异常对象中得到所有消息.

3.如果只是把当前异常对象重新抛出,那么printStackTrace()方法显示的将是原来异常抛出点的调用栈信息,而非重新抛出点的信息.

想要更新这个信息,可以调用fillInStackTrace()方法,这将返回一个Throwable对象,它是通过把当前调用栈信息填入原来那个异常对象而建立的.

packageexceptions;//: exceptions/Rethrowing.java//Demonstrating fillInStackTrace()

public classRethrowing {public static void f() throwsException {

System.out.println("originating the exception in f()");throw new Exception("thrown from f()");

}public static void g() throwsException {try{

f();

}catch(Exception e) {

System.out.println("Inside g(),e.printStackTrace()");

e.printStackTrace(System.out);throwe;

}

}public static void h() throwsException {try{

f();

}catch(Exception e) {

System.out.println("Inside h(),e.printStackTrace()");

e.printStackTrace(System.out);throw(Exception)e.fillInStackTrace();

}

}public static voidmain(String[] args) {try{

g();

}catch(Exception e) {

System.out.println("main: printStackTrace()");

e.printStackTrace(System.out);

}try{

h();

}catch(Exception e) {

System.out.println("main: printStackTrace()");

e.printStackTrace(System.out);

}

}

}/*Output:

originating the exception in f()

Inside g(),e.printStackTrace()

java.lang.Exception: thrown from f()

at Rethrowing.f(Rethrowing.java:7)

at Rethrowing.g(Rethrowing.java:11)

at Rethrowing.main(Rethrowing.java:29)

main: printStackTrace()

java.lang.Exception: thrown from f()

at Rethrowing.f(Rethrowing.java:7)

at Rethrowing.g(Rethrowing.java:11)

at Rethrowing.main(Rethrowing.java:29)

originating the exception in f()

Inside h(),e.printStackTrace()

java.lang.Exception: thrown from f()

at Rethrowing.f(Rethrowing.java:7)

at Rethrowing.h(Rethrowing.java:20)

at Rethrowing.main(Rethrowing.java:35)

main: printStackTrace()

java.lang.Exception: thrown from f()

at Rethrowing.h(Rethrowing.java:24)

at Rethrowing.main(Rethrowing.java:35)*///:~

三. 有可能在捕获异常后抛出了另一种异常,这么做的话,得到的效果类似于使用fillInStackTrace(),有关原来异常发生点的信息会丢失,剩下的是与新的抛出点有关的信息:

//最后的那个异常仅知道自己来自main(),而对f()一无所知

packageexceptions;//: exceptions/RethrowNew.java//Rethrow a different object from the one that was caught.

class OneException extendsException {public OneException(String s) { super(s); }

}class TwoException extendsException {public TwoException(String s) { super(s); }

}public classRethrowNew {public static void f() throwsOneException {

System.out.println("originating the exception in f()");throw new OneException("thrown from f()");

}public static voidmain(String[] args) {try{try{

f();

}catch(OneException e) {

System.out.println("Caught in inner try, e.printStackTrace()");

e.printStackTrace(System.out);throw new TwoException("from inner try");

}

}catch(TwoException e) {

System.out.println("Caught in outer try, e.printStackTrace()");

e.printStackTrace(System.out);

}

}

}/*Output:

originating the exception in f()

Caught in inner try, e.printStackTrace()

OneException: thrown from f()

at RethrowNew.f(RethrowNew.java:15)

at RethrowNew.main(RethrowNew.java:20)

Caught in outer try, e.printStackTrace()

TwoException: from inner try

at RethrowNew.main(RethrowNew.java:25)*///:~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值