在 Java 中重新抛出异常而不丢失堆栈跟踪
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1097527/
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
Rethrowing exceptions in Java without losing the stack trace
提问by ripper234
In C#, I can use the throw;
statement to rethrow an exception while preserving the stack trace:
在 C# 中,我可以使用该throw;
语句重新抛出异常,同时保留堆栈跟踪:
try
{
...
}
catch (Exception e)
{
if (e is FooException)
throw;
}
Is there something like this in Java (that doesn't lose the original stack trace)?
Java中是否有类似的东西(不会丢失原始堆栈跟踪)?
采纳答案by Brian Agnew
catch (WhateverException e) {
throw e;
}
will simply rethrow the exception you've caught (obviously the surrounding method has to permit this via its signature etc.). The exception will maintain the original stack trace.
将简单地重新抛出您捕获的异常(显然,周围的方法必须通过其签名等来允许这一点)。异常将保留原始堆栈跟踪。
回答by alves
In Java is almost the same:
在 Java 中几乎相同:
try
{
...
}
catch (Exception e)
{
if (e instanceof FooException)
throw e;
}
回答by David M
In Java, you just throw the exception you caught, so throw e
rather than just throw
. Java maintains the stack trace.
在 Java 中,您只需抛出捕获的异常,throw e
而不是仅抛出throw
. Java 维护堆栈跟踪。
回答by Markus Lausberg
I would prefer:
我会选择:
try
{
...
}
catch (FooException fe){
throw fe;
}
catch (Exception e)
{
// Note: don't catch all exceptions like this unless you know what you
// are doing.
...
}
回答by Markus Lausberg
something like this
像这样的东西
try
{
...
}
catch (FooException e)
{
throw e;
}
catch (Exception e)
{
...
}
回答by Olvagor
You can also wrap the exception in another one AND keep the original stack trace by passing in the Exception as a Throwable as the cause parameter:
您还可以将异常包装在另一个异常中并通过将异常作为 Throwable 作为原因参数传入来保留原始堆栈跟踪:
try
{
...
}
catch (Exception e)
{
throw new YourOwnException(e);
}
回答by Daniel
public int read(byte[] a) throws IOException {
try {
return in.read(a);
} catch (final Throwable t) {
/* can do something here, like in=null; */
throw t;
}
}
This is a concrete example where the method throws an IOException
. The final
means t
can only hold an exception thrown from the try block. Additional reading material can be found hereand here.
这是一个具体示例,其中该方法抛出IOException
. 该final
手段t
只能持有从 try 块抛出的异常。可以在此处和此处找到其他阅读材料。
回答by Sindre
Stack trace is prserved if you wrap the catched excetion into an other exception (to provide more info) or if you just rethrow the catched excetion.
如果您将捕获的异常包装到其他异常中(以提供更多信息),或者您只是重新抛出捕获的异常,则保留堆栈跟踪。
try{
...
}catch (FooException e){
throw new BarException("Some usefull info", e);
}
try{
...
}catch (FooException e){
throw new BarException("Some usefull info", e);
}
回答by Matthias
I was just having a similar situation in which my code potentially throws a number of different exceptions that I just wanted to rethrow. The solution described above was not working for me, because Eclipse told me that throw e;
leads to an unhandeled exception, so I just did this:
我只是遇到了类似的情况,我的代码可能会抛出许多我只想重新抛出的不同异常。上面描述的解决方案对我不起作用,因为 Eclipse 告诉我这throw e;
会导致未处理的异常,所以我只是这样做了:
try
{
...
} catch (NoSuchMethodException | SecurityException | IllegalAccessException e) {
throw new RuntimeException(e.getClass().getName() + ": " + e.getMessage() + "\n" + e.getStackTrace().toString());
}
Worked for me....:)
为我工作....:)