Java Throwable setStackTrace()方法与示例
Throwable類setStackTrace()方法 (Throwable Class setStackTrace() method)
setStackTrace() Method is available in java.lang package.
setStackTrace()方法在java.lang包中可用。
setStackTrace() Method is used to sets stack trace elements that will be retrieved by using getStackTrace() method.
setStackTrace()方法用于設置將通過使用getStackTrace()方法檢索的堆棧跟蹤元素。
setStackTrace() Method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
setStackTrace()方法是一個非靜態方法,只能通過類對象訪問,如果嘗試使用類名稱訪問該方法,則會收到錯誤消息。
setStackTrace() Method may throw an exception at the time of setting stack trace elements.
setStackTrace()方法在設置堆棧跟蹤元素時可能會引發異常。
NullPointerException – This exception may throw when the given argument is null or when any of its existing elements of the stack trace is null.
NullPointerException-當給定參數為null或堆棧跟蹤的任何現有元素為null時,可能引發此異常。
Syntax:
句法:
public void setStackTrace(StackTraceElement[] st_tr);Parameter(s):
參數:
StackTraceElement[] st_tr – represents an array of "StackTraceElement".
StackTraceElement [] st_tr –表示“ StackTraceElement”的數組。
Return value:
返回值:
The return type of the method is void, it returns nothing.
該方法的返回類型為void ,不返回任何內容。
Example:
例:
// Java program to demonstrate the example // of void setStackTrace(StackTraceElement[] st_tr) // method of Throwable public class SetStackTrace {public static void main(String args[]) {try {stackTraceMethod();} catch (Throwable ex) {// By using getStackTrace() method is to get the// stack trace elementStackTraceElement[] st_tr = ex.getStackTrace();System.err.println("st_tr[0].toString()" + st_tr[0].toString());}}public static void stackTraceMethod() throws Throwable {int li_nu = 7;// instantiate a new exception called // ThrowableThrowable th = new Throwable("Raise New Exception");StackTraceElement[] st_tr = new StackTraceElement[] {new StackTraceElement("cl_na", "me_na", "fu_na", li_nu)};// By using setStackTrace() method is to set// the elements in stackth.setStackTrace(st_tr);throw th;} }Output
輸出量
st_tr[0].toString()cl_na.me_na(fu_na:7)翻譯自: https://www.includehelp.com/java/throwable-setstacktrace-method-with-example.aspx
總結
以上是生活随笔為你收集整理的Java Throwable setStackTrace()方法与示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 窗帘店需要多少钱啊?
- 下一篇: 吴忠治疗输卵管积液最好的医院推荐