當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Throwable setStackTrace()用法及代碼示例


Throwable類的setStackTrace(StackTraceElement [] stackTrace)方法用於將堆棧跟蹤元素設置為此可拋出對象,並且該堆棧跟蹤將由getStackTrace()返回並由printStackTrace()和相關方法打印。使用此方法,用戶可以覆蓋默認的堆棧跟蹤,該默認堆棧跟蹤在構造throwable時由fillInStackTrace()生成,或者在從序列化流中讀取throwable時反序列化。
如果任何Throwable的堆棧跟蹤均不可寫,則調用此方法除了驗證其參數外沒有其他作用。

用法:

public void 
    setStackTrace(StackTraceElement[] stackTrace)

參數:此方法僅接受一個參數stackTrace,這是與此Throwable關聯的堆棧跟蹤元素。


返回值:此方法不返回任何結果。

以下示例程序旨在說明setStackTrace()方法:

示例1:

// Java program to demonstrate 
// the setStackTrace () Method. 
  
import java.io.*; 
  
class GFG { 
  
    // Main Method 
    public static void main(String[] args) 
        throws Exception 
    { 
  
        try { 
  
            testException1(); 
        } 
  
        catch (Throwable e) { 
  
            // access to the stack trace 
            StackTraceElement[] trace = e.getStackTrace(); 
  
            System.out.println(trace[0].toString()); 
        } 
    } 
  
    // method which throws Exception 
    public static void testException1() 
        throws Exception 
    { 
  
        // create a new Exception 
        Exception ex = new Exception(); 
  
        StackTraceElement[] trace = new StackTraceElement[] { 
            new StackTraceElement("ClassNameOfExe", 
                                  "methodNameOfExe", 
                                  "fileNameOfExe", 
                                  10) 
        }; 
  
        // sets the stack trace elements 
        ex.setStackTrace(trace); 
  
        // throw the Throwable[ 
        throw ex; 
    } 
}
輸出:
ClassNameOfExe.methodNameOfExe(fileNameOfExe:10)

示例2:

// Java program to demonstrate 
// the setStackTrace () Method. 
  
import java.io.*; 
  
class GFG { 
  
    // Main Method 
    public static void main(String[] args) 
        throws Exception 
    { 
  
        try { 
  
            Exceptiontest(); 
        } 
  
        catch (Throwable e) { 
  
            // access to the stack trace 
            StackTraceElement[] trace = e.getStackTrace(); 
  
            System.out.println("StackTraceElement length :"
                               + trace.length); 
  
            for (int i = 0; i < trace.length; i++) { 
  
                System.out.println("Stack Trace at index "
                                   + i + " : "
                                   + trace[i]); 
            } 
        } 
    } 
  
    // method which throws Exception 
    public static void Exceptiontest() 
        throws Exception 
    { 
  
        // create a new Exception 
        ArrayStoreException ex = new ArrayStoreException(); 
  
        StackTraceElement[] trace = new StackTraceElement[] { 
            new StackTraceElement("ClassName1", "methodName1", 
                                  "fileName1", 10), 
            new StackTraceElement("ClassName2", "methodName2", 
                                  "fileName2", 20), 
            new StackTraceElement("ClassName3", "methodName3", 
                                  "fileName3", 14) 
        }; 
  
        // sets the stack trace elements 
        ex.setStackTrace(trace); 
  
        throw ex; 
    } 
}
輸出:
StackTraceElement length :3
Stack Trace at index 0 : ClassName1.methodName1(fileName1:10)
Stack Trace at index 1 : ClassName2.methodName2(fileName2:20)
Stack Trace at index 2 : ClassName3.methodName3(fileName3:14)

參考文獻:



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Throwable setStackTrace() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。