Java Reference
In-Depth Information
17
// obtain the stack-trace information
18
StackTraceElement[] traceElements = exception.getStackTrace();
19
20
System.out.printf( "%nStack trace from getStackTrace:%n" );
21
System.out.println( "Class\t\tFile\t\t\tLine\tMethod" );
22
23
// loop through traceElements to get exception description
24
for (StackTraceElement element : traceElements)
25
{
26
System.out.printf( "%s\t" ,
element.getClassName()
element.getFileName()
element.getLineNumber()
element.getMethodName()
);
27
System.out.printf( "%s\t" ,
);
28
System.out.printf( "%s\t" ,
);
29
System.out.printf( "%s%n" ,
);
30
}
31
}
32
} // end main
33
34
// call method2; throw exceptions back to main
35
public static void method1()
throws Exception
36
{
37
method2();
38
}
39
40
// call method3; throw exceptions back to method1
41
public static void method2()
throws Exception
42
{
43
method3();
44
}
45
46
// throw Exception back to method2
47
public static void method3()
throws Exception
48
{
49
throw new Exception( "Exception thrown in method3" );
50
}
51
} // end class UsingExceptions
Exception thrown in method3
java.lang.Exception: Exception thrown in method3
at UsingExceptions.method3(UsingExceptions.java:49)
at UsingExceptions.method2(UsingExceptions.java:43)
at UsingExceptions.method1(UsingExceptions.java:37)
at UsingExceptions.main(UsingExceptions.java:10)
Stack trace from getStackTrace:
Class File Line Method
UsingExceptions UsingExceptions.java 49 method3
UsingExceptions UsingExceptions.java 43 method2
UsingExceptions UsingExceptions.java 37 method1
UsingExceptions UsingExceptions.java 10 main
Fig. 11.6 | Stack unwinding and obtaining data from an exception object. (Part 2 of 2.)
Search WWH ::




Custom Search