Java 代码之自定义异常

异常处理类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.Objects;

/**
* 异常信息.
*
* @author gaop
*/
public class ExceptionUtil extends RuntimeException {
private static final long serialVersionUID = 1L;

private static final String SEPERTER = " - ";

// 提示信息,专用于前台展示
private Object tip;

/******************** Constructor. ********************/
private ExceptionUtil(String errorMessage, Object tip) {
super(errorMessage);
this.tip = tip;
}

private ExceptionUtil(String errorMessage, Throwable cause) {
super(String.join(SEPERTER, getMessage(errorMessage), getMessage(cause)), cause);
}

/******************** Constructor builder. ********************/
/**
* 获取异常实例.
*
* @param message 异常信息
* @param tip 异常简单提示,专用于前台展示
* @return ExceptionUtil
*/
public static ExceptionUtil asException(String message, Object tip) {
return new ExceptionUtil(message, tip);
}

/**
* 获取异常实例.
*
* @param message 异常信息
* @return ExceptionUtil
*/
public static ExceptionUtil asException(String message) {
return new ExceptionUtil(message, null);
}

/**
* 获取异常实例.
*
* @param message 异常信息
* @param cause Throwable 异常
* @return ExceptionUtil
*/
public static ExceptionUtil asException(String message, Throwable cause) {
if (cause instanceof ExceptionUtil) {
return (ExceptionUtil)cause;
}
return new ExceptionUtil(message, cause);
}

/**
* 获取异常实例. </br>
* 使用 cause 作为异常信息</br>
*
* @param cause Throwable 异常
* @return ExceptionUtil
*/
public static ExceptionUtil asException(Throwable cause) {
if (cause instanceof ExceptionUtil) {
return (ExceptionUtil)cause;
}
return new ExceptionUtil(getMessage(cause), cause);
}

/******************** checkArgument. ********************/
/**
* 检验异常.</br>
* </br>
* 当表达式为 false 时,抛出异常.</br>
*
* @param expression 检验表达式
* @param message 异常信息
*/
public static void checkArgument(Boolean expression, String message) {
if (!expression) {
throw new ExceptionUtil(message, null);
}
}

/**
* 检验异常.</br>
* </br>
* 当表达式为 false 时,抛出异常.</br>
*
* @param expression 检验表达式
* @param msgFmt 异常信息模板,使用 String.format 占位符
* @param args 占位符信息
*/
public static void checkArgument(Boolean expression, String msgFmt, Object... args) {
checkArgument(expression, String.format(msgFmt, args));
}

/**
* 检验异常.</br>
* </br>
* 当表达式为 false 时,抛出异常.</br>
*
* @param expression 检验表达式
* @param tip 异常简单提示,专用于前台展示
* @param message 异常信息
*/
public static void checkArgument(Boolean expression, Object tip, String message) {
if (!expression) {
throw new ExceptionUtil(message, tip);
}
}

/******************** checkNonNull. ********************/
/**
* 非 null 检验.</br>
* </br>
* 当 null 空时,返回该参数</br>
*
* @param <T> 被检参数类型泛型
* @param obj 被检参数
* @param message 异常信息
* @return 被检参数
*/
public static <T> T checkNonNull(T obj, String message) {
if (Objects.isNull(obj)) {
throw new ExceptionUtil(message, null);
}
return obj;
}

/**
* 非 null 检验.</br>
* </br>
* 当非 null 时,返回该参数</br>
*
* @param <T> 被检参数类型泛型
* @param obj 被检参数
* @param msgFmt 异常信息模板,使用 String.format 占位符
* @param args 占位符信息
* @return 被检参数
*/
public static <T> T checkNonNull(T obj, String msgFmt, Object... args) {
return checkNonNull(obj, String.format(msgFmt, args));
}

/**
* 非 null 检验.</br>
* </br>
* 当非 null 时,返回该参数</br>
*
* @param <T> 被检参数类型泛型
* @param obj 被检参数
* @param tip 异常简单提示,专用于前台展示
* @param message 异常信息
* @return 被检参数
*/
public static <T> T checkNonNull(T obj, Object tip, String message) {
if (Objects.isNull(obj)) {
throw new ExceptionUtil(message, tip);
}
return obj;
}

/******************** checkNonBlank. ********************/
/**
* 非空检验.</br>
* </br>
* 当非空时,返回该参数.</br>
* 非空指非 null 且 trim 后长度大于0</br>
*
* @param <T> 被检参数类型泛型
* @param obj 被检参数
* @param message 异常信息
* @return 被检参数
*/
public static <T> T checkNonBlank(T obj, String message) {
if (Objects.isNull(obj) || 0 == String.valueOf(obj).trim().length()) {
throw new ExceptionUtil(message, null);
}
return obj;
}

/**
* 非空检验.</br>
* </br>
* 当非空时,返回该参数.</br>
* 非空指非 null 且 trim 后长度大于0</br>
*
* @param <T> 被检参数类型泛型
* @param obj 被检参数
* @param msgFmt 异常信息模板,使用 String.format 占位符
* @param args 占位符信息
* @return 被检参数
*/
public static <T> T checkNonBlank(T obj, String msgFmt, Object... args) {
return checkNonBlank(obj, String.format(msgFmt, args));
}

/**
* 非空检验.</br>
* </br>
* 当非空时,返回该参数.</br>
* 非空指非 null 且 trim 后长度大于0</br>
*
* @param <T> 被检参数类型泛型
* @param obj 被检参数
* @param tip 异常简单提示,专用于前台展示
* @param message 异常信息
* @return 被检参数
*/
public static <T> T checkNonBlank(T obj, Object tip, String message) {
if (Objects.isNull(obj) || 0 == String.valueOf(obj).trim().length()) {
throw new ExceptionUtil(message, tip);
}
return obj;
}

/******************** checkNonEmptyList. ********************/
/**
* list 非空校验.</br>
* </br>
* 当非空时,返回该 list.</br>
* 非空指 list 非 null 且包含元素</br>
*
* @param <T> 被检参数类型泛型
* @param obj 被检参数
* @param message 异常信息
* @return 被检参数
*/
public static <T> List<T> checkNonEmptyList(List<T> objs, String message) {
if (Objects.isNull(objs) || objs.isEmpty()) {
throw new ExceptionUtil(message, null);
}
return objs;
}

/******************** Object method. ********************/
/**
* 获取提示信息.
*
* @return
*/
public Object getTip() {
return this.tip;
}

/**
* 获取字符串型提示信息.
*
* @return
*/
public String getTipString() {
return String.valueOf(getTip());
}

/**
* 获取异常信息.
*
* @param obj
* @return
*/
private static String getMessage(Object obj) {
if (Objects.isNull(obj)) {
return "";
}

if (obj instanceof Throwable) {
StringWriter str = new StringWriter();
PrintWriter pw = new PrintWriter(str);
((Throwable)obj).printStackTrace(pw);
return str.toString();
} else {
return obj.toString();
}
}
}