1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package jgroup.util;
20
21 import java.io.OutputStream;
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
24 import java.io.Writer;
25
26
27
28
29
30
31
32 public class Abort
33 extends Error
34 {
35
36
37
38
39
40 private String message;
41
42 private Throwable exception;
43
44
45
46
47
48 public Abort()
49 {
50 this(null, null);
51 }
52
53 public Abort(String message)
54 {
55 this(message, null);
56 }
57
58 public Abort(Throwable exception)
59 {
60 this(null, exception);
61 }
62
63 public Abort(String message, Throwable exception)
64 {
65 this.message = message;
66 this.exception = exception;
67 }
68
69
70
71
72
73 public static Abort exit()
74 {
75 return exit(null, null, 1);
76 }
77
78 public static Abort exit(int code)
79 {
80 return exit(null, null, code);
81 }
82
83 public static Abort exit(String error)
84 {
85 return exit(error, null, 1);
86 }
87
88 public static Abort exit(String error, int code)
89 {
90 return exit(error, null, code);
91 }
92
93 public static Abort exit(Throwable exception)
94 {
95 return exit(null, exception, 1);
96 }
97
98 public static Abort exit(Throwable exception, int code)
99 {
100 return exit(null, exception, code);
101 }
102
103 public static Abort exit(String error, Throwable exception)
104 {
105 return exit(error, exception, 1);
106 }
107
108 public static Abort exit(String error, Throwable exception, int code)
109 {
110 if (error != null)
111 System.err.println(error);
112 if (exception != null)
113 exception.printStackTrace();
114 System.exit(code);
115 return new Abort("System.exit(" + code + ")");
116 }
117
118 public static String stackTrace(Throwable exception)
119 {
120 StringWriter writer = new StringWriter();
121 printStackTrace(exception, writer);
122 return writer.toString();
123 }
124
125 public static void printStackTrace(Throwable exception, StringBuffer buf)
126 {
127 buf.append(stackTrace(exception));
128 }
129
130 public static void printStackTrace(Throwable exception, OutputStream stream)
131 {
132 PrintWriter printer = new PrintWriter(stream);
133 exception.printStackTrace(printer);
134 }
135
136 public static void printStackTrace(Throwable exception, Writer writer)
137 {
138 PrintWriter printer = new PrintWriter(writer);
139 exception.printStackTrace(printer);
140 }
141
142
143
144
145
146
147
148 public static void usage(String message)
149 {
150 usage(message, 0);
151 }
152
153
154
155
156
157
158
159
160
161 public static void usage(String message, int errorcode)
162 {
163 System.err.println(message);
164 System.exit(errorcode);
165 }
166
167
168
169
170
171
172
173
174 public String toString()
175 {
176 StringWriter writer = new StringWriter();
177 if (exception != null)
178 printStackTrace(exception, writer);
179 StringBuffer buf = writer.getBuffer();
180 buf.append(getClass().getName());
181 if (message != null) {
182 buf.append(": ");
183 buf.append(message);
184 }
185 return buf.toString();
186 }
187
188 }