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


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


printStackTrace()

Java.lang.Throwable類的printStackTrace()方法用於打印此Throwable以及發生異常的其他詳細信息,例如類名和行號,表示其回溯。此方法在標準錯誤輸出流上為此Throwable對象打印堆棧跟蹤。
輸出的第一行顯示了由toString()方法為此對象返回的字符串,這意味著異常類名稱,而後幾行則表示以前由fillInStackTrace()方法記錄的數據。

用法:

public void printStackTrace()

返回值:此方法不返回任何內容。


以下示例程序旨在說明Java.lang.Throwable類的printStackTrace方法:

示例1:

// Java program to demonstrate 
// the printStackTrace () Method. 
  
import java.io.*; 
  
class GFG { 
  
    // Main Method 
    public static void main(String[] args) 
        throws Exception 
    { 
  
        try { 
  
            testException1(); 
        } 
  
        catch (Throwable e) { 
  
            // print stack trace 
            e.printStackTrace(); 
        } 
    } 
  
    // method which throws Exception 
    public static void testException1() 
        throws Exception 
    { 
  
        // create a ArrayIndexOutOfBoundsException Exception 
        ArrayIndexOutOfBoundsException 
            ae 
            = new ArrayIndexOutOfBoundsException(); 
  
        // create a new Exception 
        Exception ioe = new Exception(); 
  
        // initialize the cause and throw Exception 
        ioe.initCause(ae); 
        throw ioe; 
    } 
}
輸出:
java.lang.Exception
    at GFG.testException1(File.java:36)
    at GFG.main(File.java:15)
Caused by: java.lang.ArrayIndexOutOfBoundsException
    at GFG.testException1(File.java:32)
    ... 1 more

示例2:

// Java program to demonstrate 
// the printStackTrace () Method. 
  
import java.io.*; 
  
class GFG { 
  
    // Main Method 
    public static void main(String[] args) 
        throws Exception 
    { 
  
        try { 
  
            testException1(); 
        } 
  
        catch (Throwable e) { 
  
            // print stack trace 
            e.printStackTrace(); 
        } 
    } 
  
    // method which throws Exception 
    public static void testException1() 
        throws Exception 
    { 
  
        // create a ArrayIndexOutOfBoundsException Exception 
        ArrayIndexOutOfBoundsException 
            ae 
            = new ArrayIndexOutOfBoundsException(); 
  
        // create a new Exception 
        Exception ioe = new Exception(); 
  
        // initialize the cause and throw Exception 
        ioe.initCause(ae); 
        throw ioe; 
    } 
}
輸出:
java.lang.Exception
    at GFG.testException1(File.java:36)
    at GFG.main(File.java:15)
Caused by: java.lang.ArrayIndexOutOfBoundsException
    at GFG.testException1(File.java:32)
    ... 1 more

printStackTrace(PrintStream s)

Java.lang.Throwable類的printStackTrace(PrintStream s)方法用於打印此Throwable以及其他詳細信息,例如指定打印流發生異常的類名和行號。此方法與printStackTrace()相同,但不同之處僅在於它可以打印到作為參數傳遞的指定打印流。

用法:

public void printStackTrace(PrintStream s)

參數:此方法接受PrintStream作為參數,這是我們要在其中寫入此Throwable詳細信息的指定打印流。

返回值:此方法不返回任何內容。

下麵的程序演示了Java.lang.Throwable類的printStackTrace(PrintStream s)方法:


示例1:

// Java program to demonstrate 
// the printStackTrace(PrintStream s) Method. 
  
import java.io.*; 
  
class GFG { 
  
    // Main Method 
    public static void main(String[] args) 
        throws Exception 
    { 
        try { 
  
            // create a array of Integers 
            int[] i = new int[2]; 
  
            // try to add numbers to array 
            i[2] = 3; 
        } 
        catch (Throwable e) { 
  
            // print Stack Trace 
            e.printStackTrace(System.out); 
        } 
    } 
}
輸出:
java.lang.ArrayIndexOutOfBoundsException: 2
    at GFG.main(File.java:18)

示例2:

// Java program to demonstrate 
// the printStackTrace(PrintStream s) Method. 
  
import java.io.*; 
  
class GFG { 
  
    // Main Method 
    public static void main(String[] args) 
        throws Exception 
    { 
  
        try { 
  
            testException1(); 
        } 
  
        catch (Throwable e) { 
  
            // create printstream object 
            PrintStream 
                obj 
                = new PrintStream(System.out); 
  
            // print stack trace 
            e.printStackTrace(obj); 
        } 
    } 
  
    // method which throws Exception 
    public static void testException1() 
        throws Exception 
    { 
  
        throw new Exception("System is Down"); 
    } 
}
輸出:
java.lang.Exception: System is Down
    at GFG.testException1(File.java:35)
    at GFG.main(File.java:15)

printStackTrace(PrintWriter s)

Java.lang.Throwable類的printStackTrace(PrintWriter s)方法用於打印此Throwable以及其他詳細信息,例如類名稱和行號,指定的Print Writer發生異常。此方法與printStackTrace()相同,但區別僅在於它打印到作為參數傳遞的指定打印Writer。

用法:

public void printStackTrace(PrintWriter s)

參數:此方法接受PrintWriter s作為參數,該參數是要在其中寫入此Throwable詳細信息的指定打印作家。

返回值:此方法不返回任何內容。

下麵的程序說明Throwable類的printStackTrace(PrintWriter)方法:

示例1:

// Java program to demonstrate 
// the printStackTrace(PrintWriter s) Method. 
  
import java.io.*; 
  
class GFG { 
  
    // Main Method 
    public static void main(String[] args) 
        throws Exception 
    { 
        try { 
  
            // divide two numbers 
            int a = 74, b = 0; 
  
            int c = a / b; 
        } 
        catch (Throwable e) { 
  
            // Using a StringWriter, 
            // to convert trace into a String: 
            StringWriter sw = new StringWriter(); 
  
            // create a PrintWriter 
            PrintWriter pw = new PrintWriter(sw); 
            e.printStackTrace(pw); 
  
            String error = sw.toString(); 
  
            System.out.println("Error:\n" + error); 
        } 
    } 
}
輸出:
Error:
java.lang.ArithmeticException: / by zero
    at GFG.main(File.java:17)

示例2:

// Java program to demonstrate 
// the printStackTrace(PrintWriter s) Method. 
  
import java.io.*; 
  
class GFG { 
  
    // Main Method 
    public static void main(String[] args) 
        throws Exception 
    { 
  
        try { 
  
            testException1(); 
        } 
  
        catch (Throwable e) { 
  
            // Using a StringWriter, 
            // to convert trace into a String: 
            StringWriter sw = new StringWriter(); 
  
            // create a PrintWriter 
            PrintWriter pw = new PrintWriter(sw); 
            e.printStackTrace(pw); 
  
            String error = sw.toString(); 
  
            System.out.println("Error:\n" + error); 
        } 
    } 
  
    // method which throws Exception 
    public static void testException1() 
        throws Exception 
    { 
        throw new Exception( 
            "Waiting for input but no response"); 
    } 
}
輸出:
Error:
java.lang.Exception: Waiting for input but no response
    at GFG.testException1(File.java:38)
    at GFG.main(File.java:15)

參考文獻:
https://docs.oracle.com/javase/10/docs/api/java/lang/Throwable.html#printStackTrace()



相關用法


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