blob: a2bb9b072a5b9b52ee69a954359d648a4ef89f9f (
plain)
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
|
package hello;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
public class HelloWorld {
public void println () {
System.out.println("Hello World!");
}
private void testStackTrace () throws IOException {
try {
println();
}
catch (Exception ex) {
printRelevantStackEntries(ex,getClass().getName());
}
}
private static void printRelevantStackEntries (Exception ex, String className) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
ex.printStackTrace(ps);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
BufferedReader br = new BufferedReader(new InputStreamReader(bais));
String entry;
while ((entry = br.readLine()) != null) {
if (entry.indexOf(className) != -1) {
System.err.println(entry);
}
}
}
public static void main(String[] args) throws Exception {
new HelloWorld().testStackTrace();
}
}
|