文档章节

java异常处理基本原则

小楼听雨中
 小楼听雨中
发布于 2014/02/21 09:12
字数 1378
阅读 187
收藏 6

总原则:”Throw early catch late" principle

This is probably the most famous principle about Exception handling. It basically says that you should throw an exception as soon as you can, and catch it late as much as possible. You should wait until you have all the information to handle it properly.

This principle implicitly says that you will be more likely to throw it in the low-level methods, where you will be checking if single values are null or not appropriate. And you will be making the exception climb the stack trace for quite several levels until you reach a sufficient level of abstraction to be able to handle the problem.

 

1、什么时候抛异常

      “If your method encounters an abnormal condition that it can't handle, it should throw an exception.”

   

     判断准则:

     “Avoid using exceptions to indicate conditions that can reasonably be expected as part of the typical functioning of the method.

     (1)当方法返回值无法表达当前程序遇到的状况的时候,抛出异常(比如DataInputStream的readInt方法,返回-1,无法判断说是文件到了尽头,因为-1可能是数据的一部分)

 

2、入参校验——关于preconditions

Always validate user input in very early stage, even before it reached to actual controller. It will help you to minimize the exception handling code in your core application logic. It also helps you in making application consistent if there is some error in user input.

One design approach often discussed in the context of object-oriented programming is the Design by Contract approach. This approach to software design says that a method represents a contract between the client (the caller of the method) and the class that declares the method. The contract includes preconditions that the client must fulfill and postconditions that the method itself must fulfill.

    因而衍生出一个原则:

   “if an event represents an "abnormal condition" or a "broken contract," the thing to do in Java programs is to throw an exception.”

    对于入参的校验,采用  Bouncer Pattern 的模式进行实现(即:CheckAtTheGate模式,Security Door Pattern,google guava的preconditions就是采用此模式进行方法入参校验的)


  也可以不采用抛异常的方式,而是采用流程控制的方式,但对方法的返回值有特殊的要求(要求返回值既能承载方法本身要返回的类型,比如list,也需要能承载错误消息),对于mvc的controller返回的model and view,就是这样的数据结构,因而可以直接采用流程控制方式进行入参处理;但是servcie层则不一样,如果过度要求返回类型可以承载错误消息的话,则在设计或实现上会表现出很强的侵入性。

 

 

3、检查与非检查异常的使用原则

     “If you are throwing an exception for an abnormal condition that you feel client programmers should consciously decide how to handle, throw a checked exception.”

       “That is the trick, then, of deciding between a checked and an unchecked exception. If the abnormal condition is a failure of the method to fulfill its contract, and you feel it is common or important enough that client programmers should be forced to deal with the possibility of the exception, throw a checked exception. Otherwise, throw an unchecked exception.


   (1)checked exception  “If you throw a checked exception (and don't catch it), you will need to declare the exception in your method's throws clause. Client programmers who wish to call your method will then need to either catch and handle the exception within the body of their methods, or declare the exception in the throws clause of their methods. Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown.

   (2)unchecked exception  “With an unchecked exception, however, the compiler doesn't force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. 

 

4、异常定义的原则 : Dont Throw Generic Exceptions

      比如在抛接口调用失败的异常的时候,先细分到每个外部系统,再细分某个具体的接口。

      “One theory is that you should throw the most specific exception that still makes sense to the caller. If you need to declare a new subclass to describe the problem, so be it. It may worth creating a whole new domain-specific hierarchy of exceptions, rooted in Exception, to provide information at the appropriate level of abstraction.

      

5、异常捕获原则:

    “Let exceptions propagate higher up the call chain, rather than handling them right away.

    “Handling Exceptions As Early As Possible“  (尽早转换为自定义异常,特别是一些checked异常)

    ”Catch What You Can Handle” (根据需要,转换为自定义异常,然后在最顶层捕获处理)

 

 

6、过度使用异常——使用异常控制流程(没有必要,而且违反了PrincipleOfLeastAstonishment原则)

     Dont Use Exceptions For Flow Control

 

 

7、一些使用原则的总结

Here is a collection of the exception guidelines put forth by this article:

  • If your method encounters an abnormal condition that it can't handle, it should throw an exception.

  • Avoid using exceptions to indicate conditions that can reasonably be expected as part of the normal functioning of the method.

  • If your method discovers that the client has breached its contractual obligations (for example, by passing in bad input data), throw an unchecked exception.

  • If your method is unable to fulfill its contract, throw either a checked or unchecked exception.

  • If you are throwing an exception for an abnormal condition that you feel client programmers should consciously decide how to handle, throw a checked exception.

  • Define or choose an already existing exception class for each kind of abnormal condition that may cause your method to throw an exception.

 

 

8、best practice

(1)Never swallow the exception in catch block

(2)Declare the specific checked exceptions that your method can throw

(3)Do not catch the Exception class rather catch specific sub classes

(4)Never catch Throwable class

(5)Always correctly wrap the exceptions in custom exceptions so that stack trace is not lost

(6)Either log the exception or throw it but never do the both

(7)Never throw any exception from finally block

(8)Always catch only those exceptions that you can actually handle

(9)Don't use printStackTrace() statement or similar methods

(10)Use finally blocks instead of catch blocks if you are not going to handle exception

(11)Remember "Throw early catch late" principle

(12)Always clean up after handling the exception

(13)Throw only relevant exception from a method

(14)Never use exceptions for flow control in your program

(15)Validate user input to catch adverse conditions very early in request processing

(16)Always include all information about an exception in single log message

(17)Pass all relevant information to exceptions to make them informative as much as possible

(18)Always terminate the thread which it is interrupted

(19)Use template methods for repeated try-catch

(20)Document all exceptions in your application in javadoc

 

 

 

 

参考文献:

1、Designing with exceptions :Guidelines and tips on when and how to use exceptions


© 著作权归作者所有

小楼听雨中
粉丝 61
博文 201
码字总数 129524
作品 0
海淀
程序员
私信 提问
02《Java核心技术》之Exception和Error有什么区别?

一、提出问题 世界上存在永远不会出错的程序吗?也许这只会出现在程序员的梦中。随着编程语言和软件的诞生,异常情况就如影随形地纠缠着我们,只有正确处理好意外情况,才能保证程序的可靠性...

飞鱼说编程
2018/09/10
46
0
Java学习系列文章:说说Java中的异常

在正常的程序设计中,程序异常处理是非常关键和重要的一部分。试想一个项目中没有一个好的异常处理,这个项目会怎么样? 什么是异常 异常其实是程序上的错误,包括程序逻辑错误和系统错误。比...

java高级架构牛人
2018/06/29
13
0
异常的概念和Java异常体系结构

一、 异常的概念和Java异常体系结构 异常是程序运行过程中出现的错误。本文主要讲授的是Java语言的异常处理。Java语言的异常处理框架, 是Java语言健壮性的一个重要体现。 Java把异常当作对象...

杭州_周陶忠
2013/09/10
2.1K
0
Java异常处理 10 个最佳实践

异常处理是Java 开发中的一个重要部分。它是关乎每个应用的一个非功能性需求,是为了处理任何错误状况,比如资源不可访问,非法输入,空输入等等。Java提供了几个异常处理特性,以try,catch...

Java技术栈
2018/08/27
82
0
Java的Exception和Error面试题10问10答

在Java核心知识的面试中,你总能碰到关于 处理Exception和Error的面试题。Exception处理是Java应用开发中一个非常重要的方面,也是编写强健而稳定的Java程序的关键,这自然使它成为面试题中的...

lwei
2013/10/10
18.5K
48

没有更多内容

加载失败,请刷新页面

加载更多

插入排序算法

《Java算法总纲目录》 1、定义     元素被分为有序区和无序区两部分。最初有序区只有一个元素。每次从无序区中选择一个元素,插入到有序区的位置,直到无序区变空。 2、代码 public c...

木九天
26分钟前
3
0
ApacheCN 翻译/校对/笔记整理活动进度公告 2019.10.18

注意 请贡献者查看参与方式,然后直接在 ISSUE 中认领。 翻译/校对三个文档就可以申请当负责人,我们会把你拉进合伙人群。翻译/校对五个文档的贡献者,可以申请实习证明。 请私聊片刻(52981...

ApacheCN_飞龙
29分钟前
4
0
Hands-on! 如何给 TiDB 添加新系统表

作者:黄东旭 “TiDB,你已经是一个成熟的数据库了,该学会用自己的 SQL 查自己的状态了。” 对于一个成熟的数据库来说,通过 SQL 来查询系统本身的状态再正常不过,对于 MySQL 来说 INFOMA...

TiDB
34分钟前
4
0
SpringBoot admin+Eureka+钉钉通知

SpringBoot admin+Eureka+钉钉通知 一、效果 登录账号+密码 监控服务 查看实时日志 钉钉通知 二、什么是Spring Boot Admin ? Spring Boot Admin是一个开源社区项目,用于管理和监控SpringB...

小白的成长
38分钟前
18
0
docker-rabbitmq

docker pull rabbitmqmkdir -p /rabbitmqdocker run -d \--name rabbitmq \--hostname rabbitmq \-v /rabbitmq:/var/lib/rabbitmq \-e RABBITMQ_DEFAULT_USER=root \-e RA......

李琼涛
39分钟前
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部