Rethrowing exceptions in Java without losing the stack trace
In C#, I can use the throw;
statement to rethrow an exception while preserving the stack trace:
try
{
...
}
catch (Exception e)
{
if (e is FooException)
throw;
}
Is there something like this in Java ( that doesn't lose the original stack trace )?
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.
我会比较喜欢:
try
{
...
}
catch (FooException fe){
throw fe;
}
catch (Exception e)
{
...
}
您也可以将异常封装在另一个异常中,并通过将异常作为Throwable作为原因参数传递来保留原始堆栈跟踪:
try
{
...
}
catch (Exception e)
{
throw new YourOwnException(e);
}
链接地址: http://www.djcxy.com/p/14464.html
上一篇: 如何将堆栈跟踪转换为字符串?