捕獲到異常時,往往需要進行一些處理。比較簡單直接的方式就是打印異常棧軌跡Stack Trace。說起棧軌跡,可能很多人和我一樣,第一反應就是printStackTrace()方法。其實除了這個方法,還有一些別的內容也是和棧軌跡有關的。
1.printStackTrace()
首先需要明確,這個方法並不是來自於Exception類。Exception類本身除了定義了幾個構造器之外,所有的方法都是從其父類繼承過來的。而和異常相關的方法都是從java.lang.Throwable類繼承過來的。而printStackTrace()就是其中一個。
這個方法會將Throwable對象的棧軌跡信息打印到標準錯誤輸出流上。輸出的大體樣子如下:
1
2
3
4
|
java.lang.NullPointerException at
MyClass.mash(MyClass.java: 9 ) at
MyClass.crunch(MyClass.java: 6 ) at
MyClass.main(MyClass.java: 3 ) |
輸出的第一行是toString()方法的輸出,後面幾行的內容都是之前通過fillInStackTrace()方法保存的內容。關於這個方法,我們後面會講。
下面看一個例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class TestPrintStackTrace
{ public static void f()
throws Exception{ throw new Exception( "出問題啦!" ); } public static void g()
throws Exception{ f(); } public static void main(String[]
args) { try { g(); } catch (Exception
e) { e.printStackTrace(); } } } |
這個例子的輸出如下:
1
2
3
4
|
java.lang.Exception:
出問題啦! at
TestPrintStackTrace.f(TestPrintStackTrace.java: 3 ) at
TestPrintStackTrace.g(TestPrintStackTrace.java: 6 ) at
TestPrintStackTrace.main(TestPrintStackTrace.java: 10 ) |
在這個例子中,在方法f()中拋出異常,方法g()中調用方法f(),在main方法中捕獲異常,並且打印棧軌跡信息。因此,輸出依次展示了f—>g—>main的過程。
2.getStackTrace()方法
這個方法提供了對printStackTrace()方法所打印信息的編程訪問。它會返回一個棧軌跡元素的數組。以上面的輸出爲例,輸出的第2-4行每一行的內容對應一個棧軌跡元素。將這些棧軌跡元素保存在一個數組中。每個元素對應棧的一個棧幀。數組的第一個元素保存的是棧頂元素,也就是上面的f。最後一個元素保存的棧底元素。
下面是一個使用getStackTrace()訪問這些軌跡棧元素並打印輸出的例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class TestPrintStackTrace
{ public static void f()
throws Exception{ throw new Exception( "出問題啦!" ); } public static void g()
throws Exception{ f(); } public static void main(String[]
args) { try { g(); } catch (Exception
e) { e.printStackTrace(); System.out.println( "------------------------------" ); for (StackTraceElement
elem : e.getStackTrace()) { System.out.println(elem); } } } } |
這樣的輸出和printStackTrace()的輸出基本上是一樣的,如下:
1
2
3
4
5
6
7
|
java.lang.Exception:
出問題啦! at
TestPrintStackTrace.f(TestPrintStackTrace.java: 3 ) at
TestPrintStackTrace.g(TestPrintStackTrace.java: 6 ) at
TestPrintStackTrace.main(TestPrintStackTrace.java: 10 ) TestPrintStackTrace.f(TestPrintStackTrace.java: 3 ) TestPrintStackTrace.g(TestPrintStackTrace.java: 6 ) TestPrintStackTrace.main(TestPrintStackTrace.java: 10 ) |
3.fillInStackTrace()
我們在前面也提到了這個方法。要說清楚這個方法,首先要講一下捕獲異常之後重新拋出的問題。在catch代碼塊中捕獲到異常,打印棧軌跡,又重新throw出去。在上一級的方法調用中,再捕獲這個異常並且打印出棧軌跡信息。這兩個棧軌跡信息會一樣嗎?我們看一下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class TestPrintStackTrace
{ public static void f()
throws Exception{ throw new Exception( "出問題啦!" ); } public static void g()
throws Exception{ try { f(); } catch (Exception
e) { e.printStackTrace(); throw e; } } public static void main(String[]
args) { try { g(); } catch (Exception
e) { e.printStackTrace(); } } } |
在main方法中捕獲的異常,是在g()方法中拋出的,按理說這兩個打印棧軌跡的信息應該不同,第二次打印的信息應該沒有關於f的信息。但是事實上,兩次打印棧軌跡信息是一樣的。輸出結果如下:
1
2
3
4
5
6
7
8
|
java.lang.Exception:
出問題啦! at
TestPrintStackTrace.f(TestPrintStackTrace.java: 3 ) at
TestPrintStackTrace.g(TestPrintStackTrace.java: 7 ) at
TestPrintStackTrace.main(TestPrintStackTrace.java: 16 ) java.lang.Exception:
出問題啦! at
TestPrintStackTrace.f(TestPrintStackTrace.java: 3 ) at
TestPrintStackTrace.g(TestPrintStackTrace.java: 7 ) at
TestPrintStackTrace.main(TestPrintStackTrace.java: 16 ) |
也就是說,捕獲到異常又立即拋出,在上級方法調用中再次捕獲這個異常,打印的棧軌跡信息是一樣的。原因在於沒有將當前線程當前狀態下的軌跡棧的狀態保存進Throwabe中。現在我們引入fillInStackTrace()方法。這個方法剛好做的就是這樣的保存工作。我們看一下這個方法的原型:
1
|
public Throwable
fillInStackTrace() |
這個方法是有返回值的。返回的是保存了當前棧軌跡信息的Throwable對象。我們看看使用fillInStackTrace()方法處理後,打印的棧軌跡信息有什麼不同,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public class TestPrintStackTrace
{ public static void f()
throws Exception{ throw new Exception( "出問題啦!" ); } public static void g()
throws Exception{ try { f(); } catch (Exception
e) { e.printStackTrace(); //不要忘了強制類型轉換 throw (Exception)e.fillInStackTrace(); } } public static void main(String[]
args) { try { g(); } catch (Exception
e) { e.printStackTrace(); } } } |
輸出如下:
1
2
3
4
5
6
7
|
java.lang.Exception:
出問題啦! at
TestPrintStackTrace.f(TestPrintStackTrace.java: 3 ) at
TestPrintStackTrace.g(TestPrintStackTrace.java: 7 ) at
TestPrintStackTrace.main(TestPrintStackTrace.java: 17 ) java.lang.Exception:
出問題啦! at
TestPrintStackTrace.g(TestPrintStackTrace.java: 11 ) at
TestPrintStackTrace.main(TestPrintStackTrace.java: 17 ) |
我們看到,在main方法中打印棧軌跡已經沒有了f相關的信息了。
以上就是關於Java棧軌跡的一些我之前沒有掌握的內容,記下來備忘。
save me from myself