解析Java異常的棧軌跡及其相關(guān)方法
一.打印棧軌跡的方法
主動(dòng)調(diào)用Throwable對(duì)象的printStackTrace()=printStackTrace(System.err),printStackTrace(PrintStream),printStackTrace(PrintWriter)中的其中一個(gè)。
如果一個(gè)Exception沒有被處理,直接在main方法后面throws,程序退出前將調(diào)用異常的printStackTrace()方法,最終是Exception in thread "main" + printStackTrace()
二.棧軌跡
1、printStackTrace()
首先需要明確,這個(gè)方法并不是來自于Exception類。Exception類本身除了定義了幾個(gè)構(gòu)造器之外,所有的方法都是從其父類繼承過來的。而和異常相關(guān)的方法都是從java.lang.Throwable類繼承過來的。而printStackTrace()就是其中一個(gè)。
這個(gè)方法會(huì)將Throwable對(duì)象的棧軌跡信息打印到標(biāo)準(zhǔn)錯(cuò)誤輸出流上。輸出的大體樣子如下:
java.lang.NullPointerException at MyClass.mash(MyClass.java:9) at MyClass.crunch(MyClass.java:6) at MyClass.main(MyClass.java:3)
輸出的第一行是toString()方法的輸出,后面幾行的內(nèi)容都是之前通過fillInStackTrace()方法保存的內(nèi)容。關(guān)于這個(gè)方法,我們后面會(huì)講。
下面看一個(gè)例子:
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(); } } }
這個(gè)例子的輸出如下:
java.lang.Exception: 出問題啦! at TestPrintStackTrace.f(TestPrintStackTrace.java:3) at TestPrintStackTrace.g(TestPrintStackTrace.java:6) at TestPrintStackTrace.main(TestPrintStackTrace.java:10)
在這個(gè)例子中,在方法f()中拋出異常,方法g()中調(diào)用方法f(),在main方法中捕獲異常,并且打印棧軌跡信息。因此,輸出依次展示了f—>g—>main的過程。
2、getStackTrace()方法
這個(gè)方法提供了對(duì)printStackTrace()方法所打印信息的編程訪問。它會(huì)返回一個(gè)棧軌跡元素的數(shù)組。以上面的輸出為例,輸出的第2-4行每一行的內(nèi)容對(duì)應(yīng)一個(gè)棧軌跡元素。將這些棧軌跡元素保存在一個(gè)數(shù)組中。每個(gè)元素對(duì)應(yīng)棧的一個(gè)棧幀。數(shù)組的第一個(gè)元素保存的是棧頂元素,也就是上面的f。最后一個(gè)元素保存的棧底元素。
下面是一個(gè)使用getStackTrace()訪問這些軌跡棧元素并打印輸出的例子:
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()的輸出基本上是一樣的,如下:
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)
三.fillInStackTrace方法
native fillInStackTrace()方法將返回一個(gè)Throwable對(duì)象,它是通過把當(dāng)前調(diào)用棧信息填入原來那個(gè)異常對(duì)象兒建立的,所以返回的還是原來的異常。
調(diào)用此方法的那一行將成為異常新的發(fā)生地,有關(guān)原來異常發(fā)生點(diǎn)的信息會(huì)丟失。它的效果等價(jià)于捕獲一個(gè)異常后,重新拋出另外一種異常。兩者不同的是,fillInStackTrace后的異常還是原來的異常(只是少了棧軌跡而已);而重新拋出一個(gè)異常的話,完全跟原異常信息無關(guān)了(當(dāng)然也沒有棧軌跡)。
package com.jyz.study.jdk.exception; /** * 棧軌跡 * fillInStackTrace * @author JoyoungZhang@gmail.com * */ public class FillInStackTrace { public static void main(String[] args) throws Exception { test1(); } private static void test1() throws Exception{ try{ test2(); }catch(NullPointerException ex){ //1 throw (Exception)ex.fillInStackTrace(); //2 throw new Exception(); } } private static void test2(){ test3(); } private static void test3(){ throw new NullPointerException("str is null"); } }
1和2的異常棧信息均如圖:
不同的是this本身的信息,控制臺(tái)第一行打印的就是this。
1的棧信息
Exception in thread "main" java.lang.NullPointerException: str is null at com.jyz.study.jdk.exception.FillInStackTrace.test1(FillInStackTrace.java:20) at com.jyz.study.jdk.exception.FillInStackTrace.main(FillInStackTrace.java:13)
2的棧信息
Exception in thread "main" java.lang.Exception at com.jyz.study.jdk.exception.FillInStackTrace.test1(FillInStackTrace.java:21) at com.jyz.study.jdk.exception.FillInStackTrace.main(FillInStackTrace.java:13)
相關(guān)文章
淺談web服務(wù)器項(xiàng)目中request請(qǐng)求和response的相關(guān)響應(yīng)處理
這篇文章主要介紹了淺談web服務(wù)器項(xiàng)目中request請(qǐng)求和response的相關(guān)響應(yīng)處理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07詳解Spring Security 中的四種權(quán)限控制方式
這篇文章主要介紹了詳解Spring Security 中的四種權(quán)限控制方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10SpringBoot中REST API 接口傳參的實(shí)現(xiàn)
我們?cè)陂_發(fā)?REST API?的過程中,經(jīng)常需要傳遞參數(shù),本文主要介紹了SpringBoot中REST API 接口傳參的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12java 實(shí)現(xiàn)回調(diào)代碼實(shí)例
本文主要介紹Java的回調(diào)機(jī)制,并附實(shí)例代碼以供大家參考學(xué)習(xí),有需要的小伙伴可以看下2016-07-07使用springboot整合mybatis-plus實(shí)現(xiàn)數(shù)據(jù)庫的增刪查改示例
這篇文章主要介紹了使用springboot整合mybatis-plus實(shí)現(xiàn)數(shù)據(jù)庫的增刪查改示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Mybatis注解方式操作Oracle數(shù)據(jù)庫詳解
這篇文章主要介紹了Mybatis注解方式操作Oracle數(shù)據(jù)庫詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11Java 實(shí)戰(zhàn)項(xiàng)目之誠途旅游系統(tǒng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SpringBoot+Vue+maven+Mysql實(shí)現(xiàn)一個(gè)精美的物流管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11SpringBoot-RestTemplate如何實(shí)現(xiàn)調(diào)用第三方API
這篇文章主要介紹了SpringBoot-RestTemplate實(shí)現(xiàn)調(diào)用第三方API的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Kotlin語法學(xué)習(xí)-變量定義、函數(shù)擴(kuò)展、Parcelable序列化等簡(jiǎn)單總結(jié)
這篇文章主要介紹了Kotlin語法學(xué)習(xí)-變量定義、函數(shù)擴(kuò)展、Parcelable序列化等簡(jiǎn)單總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-05-05