不处理运行时异常通常是一个好习惯。
我有这样的场景:
/**boolean returns false if the method execution fails,
so that an error can be shown to user*/
boolean saveData()
{
try
{
//Some database calls that throw only Runtime exceptions
}
catch(Exception e)
{
//Log the exception and return false
return false;
}
return true;
}
因此从Servlet调用它会得到一个 boolean 值。如果为假,我们会显示一条消息“保存未成功”。这样可以吗,或者有更好的方法吗?
最佳答案
Is it okay to handle Runtime exceptions?
只要你当时能做一些明智的事情,就可以。这种“从不捕获运行时异常”纯粹是无稽之谈。您可能还想记录它们并做其他事情。
关于java - 可以处理运行时异常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20328609/