zoukankan      html  css  js  c++  java
  • java异常类

    基本步骤

    • 创建一个类继承于Throwable或其子类;
    • 添加构造方法;
    • 在一个方法中使用throw抛出异常;
    • 在另一个方法中捕获并处理异常
     1 /**
     2  * 权限校验失败异常(包括会话丢失)
     3  * 
     4  * 
     5  */
     6 public class FrameworkAuthenticationException extends FrameworkAppException
     7 {
     8 
     9     /**
    10      * 会话丢失
    11      */
    12     public static final String AUTHENTICATION_SESSION_LOST = "603";
    13     /**
    14      * 被踢下线
    15      */
    16     public static final String AUTHENTICATION_SESSION_KICK_OFF = "604";
    17 
    18     private static final long serialVersionUID = 3157260134403770700L;
    19 
    20     public FrameworkAuthenticationException()
    21     {
    22         super();
    23     }
    24 
    25     /**
    26      * 根据错误文本初始化异常对象,并完成查找错误信息表,生成错误信息的操作
    27      * 
    28      * @param description 错误文本
    29      */
    30     public FrameworkAuthenticationException(String description)
    31     {
    32         this(AUTHENTICATION_SESSION_LOST, description);
    33     }
    34 
    35     public FrameworkAuthenticationException(String description,
    36             Throwable rootException)
    37     {
    38         this(AUTHENTICATION_SESSION_LOST, description, rootException);
    39     }
    40 
    41     public FrameworkAuthenticationException(Throwable rootException)
    42     {
    43         this(rootException.getMessage(), rootException);
    44     }
    45 
    46     public FrameworkAuthenticationException(String code, String description)
    47     {
    48         super(code, description);
    49     }
    50 
    51     public FrameworkAuthenticationException(String code, String description,
    52             Throwable rootException)
    53     {
    54         super(code, description, rootException);
    55     }
    56 
    57     public FrameworkAuthenticationException(String code, String[] descriptions,
    58             Throwable rootException)
    59     {
    60         super(code, descriptions, rootException);
    61     }
    62 }
    异常类
      1 import org.apache.commons.lang.StringUtils;
      2 
      3 /**
      4  * 
      5  * @remark 系统最基础异常,不建议直接使用
      6  * @ 将异常基类继承自RuntimeException,以减少异常抛出代码量
      7  */
      8 public class FrameworkException extends RuntimeException {
      9 
     10     private static final long serialVersionUID = -7276002178056235933L;
     11 
     12     private String description = null;
     13 
     14     private String code = null;
     15 
     16     private Throwable rootException = null;
     17 
     18     public FrameworkException() {
     19 
     20     }
     21 
     22     /**
     23      * 使用一个异常构造此类
     24      * 
     25      * @param e原生异常
     26      */
     27     public FrameworkException(Throwable exception) {
     28         this.rootException = exception;
     29         this.description = exception.getMessage();
     30     }
     31 
     32     /**
     33      * 根据错误文本初始化异常对象,并完成查找错误信息表,生成错误信息的操作
     34      * 
     35      * @param description
     36      *            错误文本
     37      */
     38     public FrameworkException(String description) {
     39         this.description = description;
     40     }
     41 
     42     /**
     43      * 根据错误信息、根异常初始化异常对象
     44      * 
     45      * @param description
     46      *            错误文本
     47      * @param rootException
     48      *            原生异常对象
     49      */
     50     public FrameworkException(String description, Throwable rootException) {
     51         this(description);
     52         this.rootException = rootException;
     53     }
     54 
     55     /**
     56      * 根据错误代码和错误描述初始化异常对象
     57      * 
     58      * @param code
     59      *            错误代码
     60      * @param description
     61      *            错误文本
     62      */
     63     public FrameworkException(String code, String description) {
     64         this.code = code;
     65         this.description = description;
     66     }
     67 
     68     /**
     69      * 根据错误代码、错误描述、根异常初始化异常对象
     70      * 
     71      * @param code
     72      *            错误代码
     73      * @param description
     74      *            错误文本
     75      * @param rootException
     76      *            异常
     77      */
     78     public FrameworkException(String code, String description, Throwable rootException) {
     79         this(code, description);
     80         this.rootException = rootException;
     81     }
     82 
     83     public FrameworkException(String code, String[] descriptions, Throwable rootException) {
     84         this( code, descriptions != null && descriptions.length > 0 ? descriptions[0] : null, rootException);
     85     }
     86     
     87     /**
     88      * 功能:为应用级异常添加子错误描述信息 这些子错误信息会被转化成为 ActionErrors, 显示在错误页面上。
     89      * 
     90      * @param description 消息内容
     91      */
     92     public void addError(String description) {
     93         this.description = description;
     94     }
     95     
     96     public String getMessage() {
     97         return StringUtils.isBlank(description) ? super.getMessage() : description ;
     98     }
     99     
    100     public String getCode() {
    101         return code;
    102     }
    103 
    104     public String getDescription() {
    105         return description;
    106     }
    107 
    108     public void setCode(String string) {
    109         code = string;
    110     }
    111 
    112     public void setDescription(String string) {
    113         description = string;
    114     }
    115 
    116     public Throwable getRootException() {
    117         return rootException;
    118     }
    119 
    120     public void setRootException(Throwable throwable) {
    121         rootException = throwable;
    122     }
    123     /**
    124      * 功能:根据当前异常类,拼接最终展示在页面上的异常信息
    125      * 
    126      * @param description 消息内容
    127      */
    128     public String toString() {
    129         StringBuffer buffer = new StringBuffer();
    130         if (this.code != null) {
    131             buffer.append("code=");
    132             buffer.append(this.getCode());
    133         }
    134         if (this.getDescription() != null) {
    135             buffer.append(", desc=");
    136             buffer.append((this.getDescription()));
    137         }
    138         if (this.getRootException() != null) {
    139             buffer.append(", rootException is:");
    140             buffer.append(this.getRootException());
    141         }
    142         return buffer.toString();
    143 
    144     }
    145 }
    基础异常类

    这是两个异常类,只要重写toString方法,就可以自定义最终显示页面上的异常信息

  • 相关阅读:
    跨域 jQuery库ajax请求
    跨域 响应请求
    跨域 XMLHttpRequest对象
    跨域 概述
    【网易官方】极客战记(codecombat)攻略-地牢-Kithgard 橱柜 A
    【网易官方】极客战记(codecombat)攻略-地牢-橱柜里的骷髅
    【网易官方】极客战记(codecombat)攻略-地牢-逃脱
    【网易官方】极客战记(codecombat)攻略-地牢-攻破突袭
    【网易官方】极客战记(codecombat)攻略-地牢-恐惧之门
    【网易官方】极客战记(codecombat)攻略-地牢-辐射光环
  • 原文地址:https://www.cnblogs.com/javahuang/p/3154265.html
Copyright © 2011-2022 走看看