从 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);
}
}
为了响应下面的评论之一,该方法会抛出 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);
}
所以永远不会抛出 Stackoverflow 异常。