New comer to Java may throw exception in the wrong way, and the net outcome of that is very misleading error message, which can cause the user of the libraries very confused. so it is very vital to keep the exception right.
One common case of dealing message is when you are dealing calls from a lower library and you may guard against potential damage from the down-stream and throw domain specific exceptions. that menas you may need to rethrow exception in cases.
But it is easy to get it wrong. by wrong, it often meaning that the original stack is destroyed on the way to propagate to the final consumer. Let 's do a comparison on the following code.
Let 's see the code below
package Syntax.rethrow;
import java.lang.UnsupportedOperationException;
import java.lang.Exception;
public class Rethrow {
public static void generateException() {
throw new UnsupportedOperationException();
}
public static void rethrowException() throws Exception{
try {
generateException();
} catch (Exception ex) {
// you cannot swallow it
throw new Exception("Caught an exception", ex);
}
}
public static void rethrowExceptionDestroyOriginalCallstack() {
try {
generateException();
} catch (Exception ex) {
throw ex;
}
}
public static void main(String[] args) {
try {
rethrowException();
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("-=========================");
try {
rethrowExceptionDestroyOriginalCallstack();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
and when you run it, you will see error like this.
java.lang.Exception: Caught an exception
at Syntax.rethrow.Rethrow.rethrowException(Rethrow.java:18)
at Syntax.rethrow.Rethrow.main(Rethrow.java:33)
Caused by: java.lang.UnsupportedOperationException
at Syntax.rethrow.Rethrow.generateException(Rethrow.java:9)
at Syntax.rethrow.Rethrow.rethrowException(Rethrow.java:15)
... 1 more
-=========================
java.lang.UnsupportedOperationException
at Syntax.rethrow.Rethrow.generateException(Rethrow.java:9)
at Syntax.rethrow.Rethrow.rethrowExceptionDestroyOriginalCallstack(Rethrow.java:24)
at Syntax.rethrow.Rethrow.main(Rethrow.java:39)
As you can see, the
throw e;
may potentially destroy neccessary information. in this case, it is because of the UnsupportedOperationException that cause the program to fail in the first place.
so it is recommended to alwasy to throw with proper inner exception constructed.
分享到:
相关推荐
The entire Java language, including Java 7-specific features such as switch on string, try-with-resources, final rethrow, multicatch, and SafeVarargs A huge assortment of Java 7 APIs, beginning with ...
var rethrow = require ( 'rethrow' ) ; 有关用法示例,请参见。 原料药 在上下文中将给定的err重新抛出给给定lineno filename中有问题的模板表达式。 参量 err {Error} :错误对象 filename {String} :模板的...
var rethrow = require ( 'template-error' ) ; rethrow ( '\n<%= a %>\n<%= b %>' , { data : { a : 'aaa' } } ) ; 当未定义模板变量时(例如上例中的<%= b %> ,而不是得到一条神秘的错误...
测试者一掷千金 描述 不同测试人员: 将异常抛出另一个异常。 将异常抛出到catch中-相同的异常。 抛出异常到catch中-两种不同的异常。 将日志错误扔到catch中。... 将日志错误扔到catch中。... :)
//-------- From C to C++ ------------ // A simple C Program! // convert a string to uppercase! #include #define N 200 int main(){ char ms[N]; int i; printf("Input ms: "); gets(ms); for(i=0;ms[i]...
linux下使用nodejs 连接数据库mysql
java8 看不到源码SLF4J 的 FindBugs 错误模式 本产品可帮助您验证 SLF4J 1.6、1.7 和 1.8 的使用情况。 适用于 Java8 及更高版本。 要将此插件与 SonarQube 一起使用,请参阅 。 要在编译时检测问题,请参阅 。 动机...
Testcase for proper handling of rethrow.
巴别塔尝试捕获装载机该加载器使用babel转换文件并使用try catch子句包装所有函数(包括类方法和箭头函数),它收集特定信息:文件名,函数名,行号,使用AST... //a temp directory relate to project root dirctory,
% 索引错误% 抓住% % 可能在这里做一些家务% rethrow(lasterror); % 结尾% % >> 测试错误% ??? 试图访问 a(4711); 索引越界... % % >> 分散% ??? 试图访问 a(4711); 索引越界... % Error in ==> testdisperror at...
错误处理基于异常的错误处理依赖于std :: current_exception和std :: rethrow_exception。 如果您预计会遇到许多错误情况,这些速度可能会很慢。其他实施std :: future 理想情况下,我们可以将核心库功能用于期货...