1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.util;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24
25
26
27
28
29
30 @SuppressWarnings("serial")
31 public class MultiException extends Exception
32 {
33 private List<Throwable> nested;
34
35
36 public MultiException()
37 {
38 super("Multiple exceptions");
39 }
40
41
42 public void add(Throwable e)
43 {
44 if (e==null)
45 throw new IllegalArgumentException();
46
47 if(nested == null)
48 {
49 initCause(e);
50 nested = new ArrayList<>();
51 }
52 else
53 addSuppressed(e);
54
55 if (e instanceof MultiException)
56 {
57 MultiException me = (MultiException)e;
58 nested.addAll(me.nested);
59 }
60 else
61 nested.add(e);
62 }
63
64
65 public int size()
66 {
67 return (nested ==null)?0:nested.size();
68 }
69
70
71 public List<Throwable> getThrowables()
72 {
73 if(nested == null)
74 return Collections.emptyList();
75 return nested;
76 }
77
78
79 public Throwable getThrowable(int i)
80 {
81 return nested.get(i);
82 }
83
84
85
86
87
88
89
90
91 public void ifExceptionThrow()
92 throws Exception
93 {
94 if(nested == null)
95 return;
96
97 switch (nested.size())
98 {
99 case 0:
100 break;
101 case 1:
102 Throwable th=nested.get(0);
103 if (th instanceof Error)
104 throw (Error)th;
105 if (th instanceof Exception)
106 throw (Exception)th;
107 default:
108 throw this;
109 }
110 }
111
112
113
114
115
116
117
118
119
120
121 public void ifExceptionThrowRuntime()
122 throws Error
123 {
124 if(nested == null)
125 return;
126
127 switch (nested.size())
128 {
129 case 0:
130 break;
131 case 1:
132 Throwable th=nested.get(0);
133 if (th instanceof Error)
134 throw (Error)th;
135 else if (th instanceof RuntimeException)
136 throw (RuntimeException)th;
137 else
138 throw new RuntimeException(th);
139 default:
140 throw new RuntimeException(this);
141 }
142 }
143
144
145
146
147
148
149
150 public void ifExceptionThrowMulti()
151 throws MultiException
152 {
153 if(nested == null)
154 return;
155
156 if (nested.size()>0)
157 throw this;
158 }
159
160
161 @Override
162 public String toString()
163 {
164 StringBuilder str = new StringBuilder();
165 str.append(MultiException.class.getSimpleName());
166 if((nested == null) || (nested.size()<=0)) {
167 str.append("[]");
168 } else {
169 str.append(nested);
170 }
171 return str.toString();
172 }
173
174 }