Java异常之自定义异常(2)

1 package com.czgo.exception; 2 3 /** 4 * 自定义异常类(继承运行时异常) 5 * @author AlanLee 6 * @version 2016/11/26 7 */ 8 public class MyException extends RuntimeException { 9 10 private static final long serialVersionUID = 1L; 11 12 /** 13 * 错误编码 14 */ 15 private String errorCode; 16 17 /** 18 * 消息是否为属性文件中的Key 19 */ 20 private boolean propertiesKey = true; 21 22 /** 23 * 构造一个基本异常. 24 * 25 * @param message 26 * 信息描述 27 */ 28 public MyException(String message) 29 { 30 super(message); 31 } 32 33 /** 34 * 构造一个基本异常. 35 * 36 * @param errorCode 37 * 错误编码 38 * @param message 39 * 信息描述 40 */ 41 public MyException(String errorCode, String message) 42 { 43 this(errorCode, message, true); 44 } 45 46 /** 47 * 构造一个基本异常. 48 * 49 * @param errorCode 50 * 错误编码 51 * @param message 52 * 信息描述 53 */ 54 public MyException(String errorCode, String message, Throwable cause) 55 { 56 this(errorCode, message, cause, true); 57 } 58 59 /** 60 * 构造一个基本异常. 61 * 62 * @param errorCode 63 * 错误编码 64 * @param message 65 * 信息描述 66 * @param propertiesKey 67 * 消息是否为属性文件中的Key 68 */ 69 public MyException(String errorCode, String message, boolean propertiesKey) 70 { 71 super(message); 72 this.setErrorCode(errorCode); 73 this.setPropertiesKey(propertiesKey); 74 } 75 76 /** 77 * 构造一个基本异常. 78 * 79 * @param errorCode 80 * 错误编码 81 * @param message 82 * 信息描述 83 */ 84 public MyException(String errorCode, String message, Throwable cause, boolean propertiesKey) 85 { 86 super(message, cause); 87 this.setErrorCode(errorCode); 88 this.setPropertiesKey(propertiesKey); 89 } 90 91 /** 92 * 构造一个基本异常. 93 * 94 * @param message 95 * 信息描述 96 * @param cause 97 * 根异常类(可以存入任何异常) 98 */ 99 public MyException(String message, Throwable cause) 100 { 101 super(message, cause); 102 } 103 104 public String getErrorCode() 105 { 106 return errorCode; 107 } 108 109 public void setErrorCode(String errorCode) 110 { 111 this.errorCode = errorCode; 112 } 113 114 public boolean isPropertiesKey() 115 { 116 return propertiesKey; 117 } 118 119 public void setPropertiesKey(boolean propertiesKey) 120 { 121 this.propertiesKey = propertiesKey; 122 } 123 124 }

使用自定义异常抛出异常信息:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/04355f7a064225e51a2fbbda7d95a348.html