从catch块抛出异常只是为了记录消息是否有意义,以便我们确定导致异常的原因是什么?
码
public void saveLogs(Logs logs) throws RemoteException
{
try
{
LogsOps.saveLogs(logs);
}
catch (RemoteException e)
{
log.info("RemoteException is thrown while trying to save logs ", e);
throw new RemoteException("RemoteException caused while trying to save", e);
}
}
Run Code Online (Sandbox Code Playgroud)
响应下面的一条评论,这个方法会抛出StackOverFlow异常,这里只是显示那些错误的log.info的实际实现.
/** Log the message and the exception with a level of INFO.
* @param message - The message text.
* @param t - An exception to display.
*/
public void info(Object message, Throwable t)
{
String nullSafeMessage = (message != null) ? message.toString() : t.getClass().getSimpleName();
log.info(nullSafeMessage, t);
}
Run Code Online (Sandbox Code Playgroud)
所以永远不会抛出Stackoverflow异常.
Jon*_*eet 10
这取决于什么会在更高的范围内捕获异常.如果没有其他任何东西可以记录消息,那么确定,这是有道理的 - 尽管我可能会重新抛出原始异常而不是创建一个新异常:
catch (RemoteException e)
{
log.info("RemoteException is thrown while trying to save logs ", e);
throw e;
}
Run Code Online (Sandbox Code Playgroud)
但理想情况下,您catch在堆栈上方有一个块,这将适当地记录 - 如果您只是记录异常,那么无论如何都可以获得所有信息.
它可能是有意义的捉/日志/重新抛出当你想记录的信息不存在异常,如参数值.
| 归档时间: |
|
| 查看次数: |
375 次 |
| 最近记录: |