1
package exception;
2
3
import java.io.PrintWriter;
4
import java.io.Writer;
5
6
import java.util.Vector;
7
8
9
/**
10
*
11
*
12
* @author log4j
13
* @email
14
*/
15
public class ThrowableInformation implements java.io.Serializable {
16
17
//~ Static fields/initializers =============================================
18
19
static final long serialVersionUID = -4748765566864322735L;
20
21
//~ Instance fields ========================================================
22
23
private transient Throwable throwable;
24
private String[] rep;
25
26
//~ Constructors ===========================================================
27
28
public ThrowableInformation(Throwable throwable) {
29
30
this.throwable = throwable;
31
}
32
33
//~ Methods ================================================================
34
35
public Throwable getThrowable() {
36
37
return throwable;
38
}
39
40
public String[] getThrowableStrRep() {
41
42
if (rep != null) {
43
44
return (String[]) rep.clone();
45
} else {
46
47
VectorWriter vw = new VectorWriter();
48
throwable.printStackTrace(vw);
49
rep = vw.toStringArray();
50
51
return rep;
52
}
53
}
54
}
55
56
57
/**
58
* VectorWriter is a seemingly trivial implemtantion of PrintWriter.
59
* The throwable instance that we are trying to represnt is asked to
60
* print itself to a VectorWriter.
61
*
62
* By our design choice, r string representation of the throwable
63
* does not contain any line separators. It follows that println()
64
* methods of VectorWriter ignore the 'ln' part.
65
* */
66
class VectorWriter extends PrintWriter {
67
68
//~ Instance fields ========================================================
69
70
private Vector v;
71
72
//~ Constructors ===========================================================
73
74
VectorWriter() {
75
super(new NullWriter());
76
v = new Vector();
77
}
78
79
//~ Methods ================================================================
80
81
public void print(Object o) {
82
83
v.addElement(o.toString());
84
}
85
86
public void print(char[] chars) {
87
88
v.addElement(new String(chars));
89
}
90
91
public void print(String s) {
92
93
v.addElement(s);
94
}
95
96
public void println(Object o) {
97
98
v.addElement(o.toString());
99
}
100
101
// JDK 1.1.x apprenly uses this form of println while in
102
// printStackTrace()
103
public void println(char[] chars) {
104
105
v.addElement(new String(chars));
106
}
107
108
public void println(String s) {
109
110
v.addElement(s);
111
}
112
113
public String[] toStringArray() {
114
115
int len = v.size();
116
String[] sa = new String[len];
117
118
for (int i = 0; i < len; i++) {
119
120
sa[i] = (String) v.elementAt(i);
121
}
122
123
return sa;
124
}
125
126
public void write(char[] chars) {
127
128
v.addElement(new String(chars));
129
}
130
131
public void write(char[] chars, int off, int len) {
132
133
v.addElement(new String(chars, off, len));
134
}
135
136
public void write(String s, int off, int len) {
137
138
v.addElement(s.substring(off, off + len));
139
}
140
141
public void write(String s) {
142
143
v.addElement(s);
144
}
145
}
146
147
148
/**
149
*
150
*
151
* @author log4j
152
* @email
153
*/
154
class NullWriter extends Writer {
155
156
//~ Methods ================================================================
157
158
public void close() {
159
160
// blank
161
}
162
163
public void flush() {
164
165
// blank
166
}
167
168
public void write(char[] cbuf, int off, int len) {
169
170
// blank
171
}
172
}
173
package exception;2

3
import java.io.PrintWriter;4
import java.io.Writer;5

6
import java.util.Vector;7

8

9
/**10
*11
*12
* @author log4j13
* @email14
*/15
public class ThrowableInformation implements java.io.Serializable {16

17
//~ Static fields/initializers =============================================18

19
static final long serialVersionUID = -4748765566864322735L;20

21
//~ Instance fields ========================================================22

23
private transient Throwable throwable;24
private String[] rep;25

26
//~ Constructors ===========================================================27

28
public ThrowableInformation(Throwable throwable) {29

30
this.throwable = throwable;31
}32

33
//~ Methods ================================================================34

35
public Throwable getThrowable() {36

37
return throwable;38
}39

40
public String[] getThrowableStrRep() {41

42
if (rep != null) {43

44
return (String[]) rep.clone();45
} else {46

47
VectorWriter vw = new VectorWriter();48
throwable.printStackTrace(vw);49
rep = vw.toStringArray();50

51
return rep;52
}53
}54
}55

56

57
/**58
* VectorWriter is a seemingly trivial implemtantion of PrintWriter.59
* The throwable instance that we are trying to represnt is asked to60
* print itself to a VectorWriter.61
*62
* By our design choice, r string representation of the throwable63
* does not contain any line separators. It follows that println()64
* methods of VectorWriter ignore the 'ln' part.65
* */66
class VectorWriter extends PrintWriter {67

68
//~ Instance fields ========================================================69

70
private Vector v;71

72
//~ Constructors ===========================================================73

74
VectorWriter() {75
super(new NullWriter());76
v = new Vector();77
}78

79
//~ Methods ================================================================80

81
public void print(Object o) {82

83
v.addElement(o.toString());84
}85

86
public void print(char[] chars) {87

88
v.addElement(new String(chars));89
}90

91
public void print(String s) {92

93
v.addElement(s);94
}95

96
public void println(Object o) {97

98
v.addElement(o.toString());99
}100

101
// JDK 1.1.x apprenly uses this form of println while in102
// printStackTrace()103
public void println(char[] chars) {104

105
v.addElement(new String(chars));106
}107

108
public void println(String s) {109

110
v.addElement(s);111
}112

113
public String[] toStringArray() {114

115
int len = v.size();116
String[] sa = new String[len];117

118
for (int i = 0; i < len; i++) {119

120
sa[i] = (String) v.elementAt(i);121
}122

123
return sa;124
}125

126
public void write(char[] chars) {127

128
v.addElement(new String(chars));129
}130

131
public void write(char[] chars, int off, int len) {132

133
v.addElement(new String(chars, off, len));134
}135

136
public void write(String s, int off, int len) {137

138
v.addElement(s.substring(off, off + len));139
}140

141
public void write(String s) {142

143
v.addElement(s);144
}145
}146

147

148
/**149
*150
*151
* @author log4j152
* @email153
*/154
class NullWriter extends Writer {155

156
//~ Methods ================================================================157

158
public void close() {159

160
// blank161
}162

163
public void flush() {164

165
// blank166
}167

168
public void write(char[] cbuf, int off, int len) {169

170
// blank171
}172
}173

上面代码的功能可以完成把一个throwable的对象的printStack变成String表现形式,可以提供更好的表现,比如email这段String或者在调试界面显示整个String,而不仅仅是在控制台打印.:)
下面的是我的ApplicationException的基类,如果是SystemException只要继承自RuntimeException就行了:
1
package exception;
2
3
import org.apache.log4j.Logger;
4
5
import java.io.PrintStream;
6
import java.io.PrintWriter;
7
8
9
/**
10
*
11
*
12
* @author Ivan Jiang
13
*/
14
public class BaseException extends Exception {
15
16
//~ Static fields/initializers =============================================
17
18
private static final Logger logger = Logger.getLogger(BaseException.class);
19
20
//~ Instance fields ========================================================
21
22
private Throwable rootCause;
23
24
//~ Constructors ===========================================================
25
26
public BaseException() {
27
super();
28
}
29
30
public BaseException(String s) {
31
this(s, null);
32
//rootCause = this;
33
}
34
35
public BaseException(String s, Throwable e) {
36
super(s);
37
38
if (e instanceof BaseException) {
39
40
rootCause = ((BaseException) e).rootCause;
41
} else {
42
43
rootCause = e;
44
}
45
46
//logger.info(s, e);
47
}
48
49
public BaseException(Throwable e) {
50
this("", e);
51
}
52
53
//~ Methods ================================================================
54
55
public Throwable getRootCause() {
56
57
return rootCause;
58
}
59
60
public void printStackTrace() {
61
62
printStackTrace(System.err);
63
}
64
65
public void printStackTrace(PrintStream outStream) {
66
67
printStackTrace(new PrintWriter(outStream));
68
}
69
70
public void printStackTrace(PrintWriter writer) {
71
72
super.printStackTrace(writer);
73
74
if (getRootCause() != null) {
75
76
getRootCause()
77
.printStackTrace(writer);
78
}
79
}
80
}
81
package exception;2

3
import org.apache.log4j.Logger;4

5
import java.io.PrintStream;6
import java.io.PrintWriter;7

8

9
/**10
*11
*12
* @author Ivan Jiang13
*/14
public class BaseException extends Exception {15

16
//~ Static fields/initializers =============================================17

18
private static final Logger logger = Logger.getLogger(BaseException.class);19

20
//~ Instance fields ========================================================21

22
private Throwable rootCause;23

24
//~ Constructors ===========================================================25

26
public BaseException() {27
super();28
}29

30
public BaseException(String s) {31
this(s, null);32
//rootCause = this;33
}34

35
public BaseException(String s, Throwable e) {36
super(s);37

38
if (e instanceof BaseException) {39

40
rootCause = ((BaseException) e).rootCause;41
} else {42

43
rootCause = e;44
}45

46
//logger.info(s, e);47
}48

49
public BaseException(Throwable e) {50
this("", e);51
}52

53
//~ Methods ================================================================54

55
public Throwable getRootCause() {56

57
return rootCause;58
}59

60
public void printStackTrace() {61

62
printStackTrace(System.err);63
}64

65
public void printStackTrace(PrintStream outStream) {66

67
printStackTrace(new PrintWriter(outStream));68
}69

70
public void printStackTrace(PrintWriter writer) {71

72
super.printStackTrace(writer);73

74
if (getRootCause() != null) {75

76
getRootCause()77
.printStackTrace(writer);78
}79
}80
}81

测试代码可以这么写:
1
public static void main(String[] args) {
2
3
Exception e = new Exception("test1",
4
new Exception("test2",
5
new Exception("exception")));
6
ThrowableInformation ti = new ThrowableInformation(e);
7
8
String[] s = ti.getThrowableStrRep();
9
StringBuffer buf = new StringBuffer();
10
11
if (s != null) {
12
13
for (int i = 0; i < s.length; i++) {
14
15
buf.append(s[i]);
16
buf.append("\r\n");
17
}
18
}
19
20
System.out.println(buf.toString());
21
}
public static void main(String[] args) {2

3
Exception e = new Exception("test1",4
new Exception("test2",5
new Exception("exception")));6
ThrowableInformation ti = new ThrowableInformation(e);7

8
String[] s = ti.getThrowableStrRep();9
StringBuffer buf = new StringBuffer();10

11
if (s != null) {12

13
for (int i = 0; i < s.length; i++) {14

15
buf.append(s[i]);16
buf.append("\r\n");17
}18
}19

20
System.out.println(buf.toString());21
}


浙公网安备 33010602011771号