java 重新抛出异常
一.有时希望把刚捕获的异常重新抛出,尤其时在使用Exception捕获所以异常的时候,既然已经得到了对当前异常对象的引用,可以重新把它抛出:
catch(Exception e){
System.out.println("An exception was thrown");
throw e;
}
二 :
1.重新抛出异常会把异常抛给上一级环境中的异常处理程序,同一个try块的后续catch字句将忽略.
2.异常对象的所有信息都得以保持,所以高一级环境中捕获此异常的处理程序可以从这个异常对象中得到所有消息.
3.如果只是把当前异常对象重新抛出,那么printStackTrace()方法显示的将是原来异常抛出点的调用栈信息,而非重新抛出点的信息.
想要更新这个信息,可以调用fillInStackTrace()方法,这将返回一个Throwable对象,它是通过把当前调用栈信息填入原来那个异常对象而建立的.
package exceptions;
//: exceptions/Rethrowing.java
// Demonstrating fillInStackTrace() public class Rethrowing {
public static void f() throws Exception {
System.out.println("originating the exception in f()");
throw new Exception("thrown from f()");
}
public static void g() throws Exception {
try {
f();
} catch(Exception e) {
System.out.println("Inside g(),e.printStackTrace()");
e.printStackTrace(System.out);
throw e;
}
}
public static void h() throws Exception {
try {
f();
} catch(Exception e) {
System.out.println("Inside h(),e.printStackTrace()");
e.printStackTrace(System.out);
throw (Exception)e.fillInStackTrace();
}
}
public static void main(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()一无所知
package exceptions;
//: exceptions/RethrowNew.java
// Rethrow a different object from the one that was caught. class OneException extends Exception {
public OneException(String s) { super(s); }
} class TwoException extends Exception {
public TwoException(String s) { super(s); }
} public class RethrowNew {
public static void f() throws OneException {
System.out.println("originating the exception in f()");
throw new OneException("thrown from f()");
}
public static void main(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)
*///:~
java 重新抛出异常的更多相关文章
- Java 如何抛出异常、自定义异常
Java错误与异常的基本概念: 1.java中异常均继承自Throwable,其有两个重要的直接子类error与exception. 2.java错误error,大部分是由虚拟机爆出来的错误,是程序无 ...
- Java 如何抛出异常、自定义异常、手动或主动抛出异常
public static void main(String[] args) { try { throw new Exception("抛出异常"); } catch (Excep ...
- Java内部抛出异常外部不能catch问题分析
今天在论坛看到一篇关于异常处理的文章,异常处理机制详解开头就搬出了这样一个例子: public class TestException { public TestException() { } boo ...
- [Java 进阶]异常
异常:程序在运行过程中发生由于硬件设备问题.软件设计错误等导致的程序异常事件. 世上没有百分之百完美的程序.程序总难免存在各式各样的问题.所以,程序中添加对于错误的处理机制是十分有必要的.这就好比人多 ...
- [转载]java中try 与catch的使用
留着以后看 原文地址:与catch的使用">java中try 与catch的使用作者:碌碌如玉 try{ //代码区 }catch(Exception e){ //异常处理 } 代码区 ...
- java的异常处理机制(try…catch…finally)
1 引子try…catch…finally恐怕是大家再熟悉不过的语句了,而且感觉用起来也是很简单,逻辑上似乎也是很容易理解.不过,我亲自体验的“教训”告诉我,这个东西可不是想象中的那么简单.听话.不信 ...
- Java调用Python脚本
今天遇到Java调用一个Python脚本的问题,纠结了大半天,遇到各种问题.网上搜索的大部分都是用jython,但是我想要调用的python脚本里有import urllib,这个urllib也不是什 ...
- java中try 与catch的使用
(2011-10-08 17:08:43) 转载▼ 标签: 杂谈 分类: Java try{//代码区}catch(Exception e){//异常处理}代码区如果有错误,就会返回所写异常的处理. ...
- 【Java学习笔记之三十三】详解Java中try,catch,finally的用法及分析
这一篇我们将会介绍java中try,catch,finally的用法 以下先给出try,catch用法: try { //需要被检测的异常代码 } catch(Exception e) { //异常处 ...
随机推荐
- BZOJ2622 深入虎穴(最短路径)
如果对某个点能求出与其相邻的所有点到达出口的最短时间,那么该点的答案就可以在其中取次小值了. 对于dijkstra魔改一下就能做到这个.初始时将所有出口的最短时间设为0并放入堆,记录最短和次短路径,每 ...
- Java 8新特性之接口改善(八恶人-1)
Daisy Donergue 多莫歌·黛西 “By woman, you mean her?” 她也能叫女人? Java 8在13年9月发布,写这篇博文的时间已经是17年12月份了.来的有点晚,但是有 ...
- 【Revit API】FamilyInstance、FamilySymbol、Family的寻找关系
话不多说,做个笔记 FamilyInstance instance; var typeid = instance.GetTypeId(); var symbol = RevitDoc.GetEleme ...
- BZOJ 4173: 数学
4173: 数学 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 462 Solved: 227[Submit][Status][Discuss] D ...
- BZOJ 2226 [Spoj 5971] LCMSum 最大公约数之和 | 数论
BZOJ 2226 [Spoj 5971] LCMSum 这道题和上一道题十分类似. \[\begin{align*} \sum_{i = 1}^{n}\operatorname{LCM}(i, n) ...
- c++中的 Stl 算法(很乱别看)
#include <iostream> #include <vector> #include <functional> #include <algorithm ...
- Python 文件解压缩
shutil对压缩包的处理是通过调用zipFile和tarFile两个模块来进行的. import zipfile # zipfile压缩 z = zipfile.ZipFile('ss.zip', ...
- Object和泛型
这俩东东呢实现的效果是一样的,但是它们俩的效率不一样,原理也不一样.直接说结论吧,泛型比Object好用,效率高.因为Object有一个装箱和拆箱的过程,倘若是很多的数据那效率就低到不行了,而泛型没有 ...
- spring cloud微服务架构 服务提供者和服务消费者
服务提供者和服务消费者 下面这张表格,简单描述了服务提供者/消费者是什么: | 名词 | 概念 | | ----- | ----------------------- | | 服务提供者 | 服务 ...
- redis添加systemctl服务
1.编辑systemctl服务配置文件 vim /lib/systemd/system/redis.service 2.内容如下 [Unit]Description=RedisAfter=networ ...