異常處理類 Throwable原始碼詳解

2022-09-09 18:42:11 字數 3017 閱讀 9246

package j**a.lang;

import j**a.io.*;

/** *

* throwable是所有error和exceptiong的父類

* 注意它有四個建構函式:

* throwable()

* throwable(string message)

* throwable(throwable cause)

* throwable(string message, throwable cause)

* */

public class throwable implements serializable

/*** 建構函式

*/public throwable(string message)

/*** 建構函式,cause表示起因物件

*/public throwable(string message, throwable cause)

/*** 建構函式

*/public throwable(throwable cause)

/*** 獲取詳細資訊

*/public string getmessage()

/*** 獲取詳細資訊

*/public string getlocalizedmessage()

/*** 獲取起因物件

*/public throwable getcause()

/*** 初始化起因物件,這個方法只能在未被初始化的情況下呼叫一次

*/public synchronized throwable initcause(throwable cause)

/*** 字串表示形式

*/public string tostring()

/*** 列印出錯誤軌跡

*/public void printstacktrace()

/*** 列印出錯誤軌跡

*/public void printstacktrace(printstream s)

}/**

* 列印起因物件的資訊

* @param s 列印的流

* @param causedtrace 有此物件引起的異常的異常軌跡

*/private void printstacktraceascause(printstream s,

stacktraceelement causedtrace)

//相同的個數

int framesincommon = trace.length - 1 - m;

//列印出不同的錯誤軌跡

s.println("caused by: " + this);

for (int i=0; i <= m; i++)

s.println("\tat " + trace[i]);

//如果有相同的則列印出相同的個數

if (framesincommon != 0)

s.println("\t... " + framesincommon + " more");

//獲得此物件的起因物件,並遞迴列印出資訊

throwable ourcause = getcause();

if (ourcause != null)

ourcause.printstacktraceascause(s, trace);

}/**

* 列印出錯誤軌跡

*/public void printstacktrace(printwriter s)

}/**

* 列印起因物件的資訊

*/private void printstacktraceascause(printwriter s,

stacktraceelement causedtrace)

int framesincommon = trace.length - 1 - m;

s.println("caused by: " + this);

for (int i=0; i <= m; i++)

s.println("\tat " + trace[i]);

if (framesincommon != 0)

s.println("\t... " + framesincommon + " more");

// recurse if we h**e a cause

throwable ourcause = getcause();

if (ourcause != null)

ourcause.printstacktraceascause(s, trace);

}/**

* 填充異常軌跡

*/public synchronized native throwable fillinstacktrace();

/*** 返回當前的異常軌跡的拷貝

*/public stacktraceelement getstacktrace()

/*** 獲取當前的異常軌跡

*/private synchronized stacktraceelement getourstacktrace()

return stacktrace;

}/**

* 設定異常軌跡

*/public void setstacktrace(stacktraceelement stacktrace)

/*** 異常軌跡的深度,0表示無法獲得

*/private native int getstacktracedepth();

/*** 獲取指定位標的異常軌跡

*/private native stacktraceelement getstacktraceelement(int index);

private synchronized void writeobject(j**a.io.objectoutputstream s)

throws ioexception

}

異常處理 Throwable

throwable是類,exception和error都繼承了該類 所以在捕捉的時候,也可以使用throwable進行捕捉 如圖 異常分error和exception exception裡又分執行時異常和可查異常。在方法宣告上,可以丟擲指定的異常,比如filenotfoundexception 那麼...

異常 Throwable中3個異常的處理方法

throwable類定義了3個異常處理方法 strng getmessage 返回此throwable的簡短描述 string tostring 返回此throwable迭代纖細訊息字串。void printstacktrace jvm列印的異常物件,預設此方法,列印的異常資訊是最全面的 publi...

定義「異常類」處理異常

include using namespace std 定義除數為0異常類 class zeroexception char show 定義總分或科目數為負數異常類 class negativeexception char show float div float score,int n if n ...