当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。