zoukankan      html  css  js  c++  java
  • Effective Java 65 Don't ignore exceptions

    Principle

    • An empty catch block defeats the purpose of exceptions, which is to force you to handle exceptional conditions.

      // Empty catch block ignores exception - Highly suspect!

      try {

      ...

      } catch (SomeException e) {

      }  

    • At the very least, the catch block should contain a comment explaining why it is appropriate to ignore the exception.

      An example of the sort of situation where it might be appropriate to ignore an exception is when closing a FileInputStream. You haven't changed the state of the file, so there's no need to perform any recovery action, and you've already read the information that you need from the file, so there's no reason to abort the operation in progress. Even in this case, it is wise to log the exception, so that you can investigate the matter if these exceptions happen often.  

      Summary

      The advice in this item applies equally to checked and unchecked exceptions. Whether an exception represents a predictable exceptional condition or a programming error, ignoring it with an empty catch block will result in a program that continues silently in the face of error. The program might then fail at an arbitrary time in the future, at a point in the code that bears no apparent relation to the source of the problem. Properly handling an exception can avert failure entirely. Merely letting an exception propagate outward can at least cause the program to fail swiftly, preserving information to aid in debugging the failure.

         

         

  • 相关阅读:
    linux学习之centos(四):git的安装
    MongoDB学习
    linux学习之centos(三):mysql数据库的安装和配置
    面经中高频知识点归纳(三)
    各编程语言的内存分配方式
    carson常用linux命令整理
    在 Linux 虚拟机中手动安装或升级 VMware Tools
    Fidder 网络抓包调试工具
    面经中高频知识点归纳(二)
    java集合类
  • 原文地址:https://www.cnblogs.com/haokaibo/p/do-not-ignore-exceptions.html
Copyright © 2011-2022 走看看