如何附加到 Java 异常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5470284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I append to a Java exception?
提问by kevinarpe
I am new to Java and exceptions in general.
我对 Java 和一般的异常不熟悉。
In my prior C/Perl programming days, when I wrote a library function, errors were passed back by a boolean flag, plus some kind of string with a human-friendly (or programmer-friendly) error message. Java and C++ have exceptions, which is handy because they include stack traces.
在我之前的 C/Perl 编程时代,当我编写一个库函数时,错误通过一个布尔标志传回,加上某种带有人类友好(或程序员友好)错误消息的字符串。Java 和 C++ 有异常,这很方便,因为它们包含堆栈跟踪。
I often find when I catch an exception, I want to add my two cents, then pass it along.
我经常发现,当我捕获异常时,我想添加我的两分钱,然后传递它。
How can this be done? I don't want to throw away the whole stack trace... I don't know how deep the failure occurred and for what reason.
如何才能做到这一点?我不想扔掉整个堆栈跟踪......我不知道失败发生的深度和原因。
I have a little utility library to convert a stack track (from an Exception object) into a string. I guess I could append this to my new exception message, but it seems like a hack.
我有一个小实用程序库来将堆栈轨道(从异常对象)转换为字符串。我想我可以将它附加到我的新异常消息中,但这似乎是一种黑客攻击。
Below is an example method. Advices?
下面是一个示例方法。建议?
public void foo(String[] input_array) {
for (int i = 0; i < input_array.length; ++i) {
String input = input_array[i];
try {
bar(input);
}
catch (Exception e) {
throw new Exception("Failed to process input ["
+ ((null == input) ? "null" : input)
+ "] at index " + i + ": " + Arrays.toString(input_array)
+ "\n" + e);
}
}
}
回答by axtavt
Exceptions can be chained:
异常可以被链接:
try {
...
} catch (Exception ex) {
throw new Exception("Something bad happened", ex);
}
It makes original exception the causeof the new one. Cause of exception can be obtained using getCause()
, and calling printStackTrace()
on the new exception will print:
它使原始异常成为新异常的原因。可以使用 获取异常原因getCause()
,调用printStackTrace()
新异常将打印:
Something bad happened ... its stacktrace ... Caused by: ... original exception, its stacktrace and causes ...
回答by Jon Skeet
Typically you throw a new exception which includes the oldexception as a "cause". Most exception classes have a constructor which accept a "cause" exception. (You can get at this via Throwable.getCause()
.)
通常,您会抛出一个新异常,其中包含旧异常作为“原因”。大多数异常类都有一个接受“原因”异常的构造函数。(您可以通过 获得此信息Throwable.getCause()
。)
Note that you should almost never be catching just Exception
- generally you should be catching a more specific type of exception.
请注意,您几乎永远不应该只捕获Exception
- 通常您应该捕获更具体类型的异常。
回答by Andreas Dolk
Just use a different constructor:
只需使用不同的构造函数:
Exception(String message, Throwable cause)
The message is your "two cent" and you include the catched exception, which will be shown in a stacktrace printout
该消息是您的“两分钱”,您包括捕获的异常,该异常将显示在堆栈跟踪打印输出中
回答by Janick Bernet
You can add the underlying cause to a newly created exception:
您可以将根本原因添加到新创建的异常中:
throw new Exception("Failed to process input ["
+ ((null == input) ? "null" : input)
+ "] at index " + i + ": " + Arrays.toString(input_array)
+ "\n" + e.getMessage(), e);