zoukankan      html  css  js  c++  java
  • 异常处理规范

    1、 Throwable是Java中所有错误和异常的超类。 error是所有错误的超类,这些错误并不意味着需要被应用程序所捕获。捕获Throwable或Error也将捕获OutOfMemoryError和InternalError,应用程序不应尝试恢复这些错误。只能捕获异常及其子类,文章最后贴上自定义的异常代码。

    2、 定义时区分unchecked / checked 异常,避免直接抛出new RuntimeException(),更不允许抛出Exception或者Throwable,应使用有业务含义的自定义异常。末尾贴上自定义的异常处理代码。

    3、 不同的一场类型会影响业务走向,所以我一般都是在service这一层统一处理收敛异常。但是在有事务的情况下,我是会将异常抛出到控制层做处理的,不然可能会影响事务的回滚。总结就是:存在事务,抛到控制层用aop作统一处理,如果不存在事务,统一在service层处理,这样方便一些。

    4、异常分类:

    Throwable

                      Error       

                      Exception

                              编译异常(受检异常),编译过程中可能发生的异常。

                                       IOException

                                       FileNotFoundException

                                       SQLException

                                       两种异常都是在运行时出错。

                              运行异常(非受检异常)RuntimeException,运行过程中发现问题。

                                       NullPointerException空指针异常

                                       IndexOutOfBoundsException下标越界

                                       ClassCastException类型转换

                                       NumberFormateException数字格式不正确

                                       ArithmeticException数学运算

    5、自定义异常代码:

    public class UserException extends RuntimeException{

             private String errorCode;

             private String errorMessage;

             private boolean forceCatch = false;

             public UserException(){

             }

             public UserException(String errorMessage){

                      super(errorMessage);

                      this.errorMessage = errorMessage;

             }

             public UserException(ResultEnum resultEnum){

                      super(resultEnum.getMessage());

                      this.errorCode = resultEnum.getCode();

                      this.errorMessage = resultEnum.getMessage();

             }

             public UserException(String errorCode,String errorMessage){

                      super(errorMessage);

                      this.errorCode = errorCode;

                      this.errorMessage = errorMessage;

             }

             public UserException(String errorMessage,Throwable throwable){

                      super(errorMessage,throwable);

                      this.errorMessage = errorMessage;

             }

             public UserException(String errorMessage,Throwable throwable,boolean forceCatch){

                      super(errorMessage,throwable);

                      this.errorMessage = errorMessage;

                      this.forceCatch = forceCatch;

             }

             public UserException(String errorCode,String errorMessage,Throwable throwable){

                      super(errorMessage,throwable);

                      this.errorCode = errorCode;

                      this.errorMessage = errorMessage;

             }

             public UserException(String errorCode,String errorMessage,Throwable throwable,boolean forceCatch){

                      super(errorMessage,throwable);

                      this.errorCode = errorCode;

                      this.errorMessage = errorMessage;

                      this.forceCatch = forceCatch;

             }

             public String getErrorCode() {

                      return errorCode;

             }

             public void setErrorCode(String errorCode) {

                      this.errorCode = errorCode;

             }

             public String getErrorMessage() {

                      return errorMessage;

             }

             public void setErrorMessage(String errorMessage) {

                      this.errorMessage = errorMessage;

             }

             public boolean isForceCatch() {

                      return forceCatch;

             }

             public void setForceCatch(boolean forceCatch) {

                      this.forceCatch = forceCatch;

             }

    }

  • 相关阅读:
    postgreSQL 时间线
    Using CSV-Format Log Output
    Understanding postgresql.conf : log*
    UNDERSTANDING POSTGRESQL.CONF: CHECKPOINT_SEGMENTS, CHECKPOINT_TIMEOUT, CHECKPOINT_WARNING
    PgSQL · 追根究底 · WAL日志空间的意外增长
    caffe源码学习
    Git 常用命令学习
    Linux系统的目录结构
    NMS 原理 了解
    nvidia-smi 查看GPU信息字段解读
  • 原文地址:https://www.cnblogs.com/wanghaichao/p/9166385.html
Copyright © 2011-2022 走看看