-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExceptionsMu.java
111 lines (98 loc) · 3.26 KB
/
ExceptionsMu.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
package mu;
import mu.exceptions.GeneralThrowableError;
import mu.exceptions.InterruptedError;
import mu.exceptions.UncheckedException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UncheckedIOException;
/**
* Exception handling helpers
* to convert checked exceptions to unchecked see <code>asUnchecked</code>
*/
public class ExceptionsMu {
/**
* 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
*/
public static RuntimeException asUnchecked(Throwable t) {
if (t instanceof RuntimeException) {
return (RuntimeException) t;
}
if (t instanceof Error) {
throw (Error) t;
}
if (t instanceof Exception) {//checked exception
if (t instanceof IOException) {
return new UncheckedIOException((IOException) t);
}
if (t instanceof InterruptedException) {
throw handleInterruptedException((InterruptedException) t);
}
throw new UncheckedException(t);
}
throw new GeneralThrowableError(t);
}
public interface NonCheckedExecutable<T> {
T execute() throws Throwable;
}
/**
* 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()
*/
public static <T> T asUnchecked(NonCheckedExecutable<T> executable) {
try {
return executable.execute();
} catch (Throwable e) {
throw asUnchecked(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
*/
public static InterruptedError handleInterruptedException(InterruptedException e) {
Thread.currentThread().interrupt();
return new InterruptedError(e);
}
/**
* @param t the throwable
* @return The root cause exception
*/
public static Throwable getRootCause(Throwable t) {
Throwable $ = t;
while ($.getCause() != null && $.getCause() != $) {
$ = $.getCause();
}
return $;
}
public static String getStackTrace(Throwable t) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
return sw.toString();
}
public static String getRootCauseMessage(Throwable t) {
return getRootCause(t).getMessage();
}
}