Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExceptionsMu.java
More file actions
Latest commit
111 lines (98 loc) · 3.26 KB
/
Copy pathExceptionsMu.java
File metadata and controls
111 lines (98 loc) · 3.26 KB
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
packagemu;
importmu.exceptions.GeneralThrowableError;
importmu.exceptions.InterruptedError;
importmu.exceptions.UncheckedException;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.io.StringWriter;
importjava.io.UncheckedIOException;
/**
* Exception handling helpers
* to convert checked exceptions to unchecked see <code>asUnchecked</code>
*/
publicclassExceptionsMu {
/**
* this method wrap checked exception by unchecked exception
* it is intended to use by client calling a code that throws checked exception in the case the client don't want to take care of the exception immediatly
* and to prevent pollution of methods signature
* example of usage:
* <code>
* try {
* ...
* } catch (IOException e) {
* throw ExceptionsMu.asUnchecked(e);
* }
* </code>
*
* @param t the throwable
* @return the <code>t</code> parameter if it is not a checked exception,
* otherwise a <code>RuntimeException</code> with <code>t</code> as its cause
*/
publicstaticRuntimeExceptionasUnchecked(Throwablet) {
if (tinstanceofRuntimeException) {
return (RuntimeException) t;
}
if (tinstanceofError) {
throw (Error) t;
}
if (tinstanceofException) {//checked exception
if (tinstanceofIOException) {
returnnewUncheckedIOException((IOException) t);
}
if (tinstanceofInterruptedException) {
throwhandleInterruptedException((InterruptedException) t);
}
thrownewUncheckedException(t);
}
thrownewGeneralThrowableError(t);
}
publicinterfaceNonCheckedExecutable<T> {
Texecute() throwsThrowable;
}
/**
* wrap a method invocation with a try/catch clause wrapping checked exception with unchecked
* example of usage:
* <code>asUnchecked(() -> {doSomething();});</code>
*
* @param executable the method to execute
* @param <T> type of return value
* @return value returned from NonCheckedExecutable.execute()
*/
publicstatic <T> TasUnchecked(NonCheckedExecutable<T> executable) {
try {
returnexecutable.execute();
} catch (Throwablee) {
throwasUnchecked(e);
}
}
/**
* <code>InterruptedException</code> signals the thread that it should stop
* so the best approach is to treat it like an <code>Error</code> and try to shutdown the thread gracefully
* The methods sets back interrupted flag and return an error wrapping the original <code>InterruptedException</code> that should be thrown
* @return InterruptedError wrapping the checked InterruptedException
*/
publicstaticInterruptedErrorhandleInterruptedException(InterruptedExceptione) {
Thread.currentThread().interrupt();
returnnewInterruptedError(e);
}
/**
* @param t the throwable
* @return The root cause exception
*/
publicstaticThrowablegetRootCause(Throwablet) {
Throwable$ = t;
while ($.getCause() != null && $.getCause() != $) {
$ = $.getCause();
}
return$;
}
publicstaticStringgetStackTrace(Throwablet) {
StringWritersw = newStringWriter();
PrintWriterpw = newPrintWriter(sw);
t.printStackTrace(pw);
returnsw.toString();
}
publicstaticStringgetRootCauseMessage(Throwablet) {
returngetRootCause(t).getMessage();
}
}