java - 可以处理运行时异常吗?

标签 java exception

不处理运行时异常通常是一个好习惯。

我有这样的场景:

  /**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/

相关文章:

java - “instanceof”的示例

java - 如何在 struts2 文本字段标签中将日历对象转换为日期

java - Google Webmaster Tools API 能否用于自动修复 URL Page Not Found 错误?

listview - 缓存图像 (ffimageloading) 在 android 上出现 OUT OF MEMORY 错误 - xamarin 表单

java - 如何跳过 block 中的任何错误并继续下一个 block 项目?

java - JavaFX tableView 中的导航

java - 双数组从每个数组索引中查找最小值必须不同

http - webMethods pub.client.http 抛出 401 错误

java - 是否可以在外部 JAR 中的方法上使用 "throws"

c++ - try catch 语法构造函数