📜  Java中的可抛出 printStackTrace() 方法及示例

📅  最后修改于: 2022-05-13 01:55:22.333000             🧑  作者: Mango

Java中的可抛出 printStackTrace() 方法及示例

打印堆栈跟踪()

用于打印此 Throwable 的Java.lang.Throwable 类printStackTrace()方法以及其他详细信息(如发生异常的类名和行号)表示其回溯。此方法在标准错误输出流上打印此 Throwable 对象的堆栈跟踪。
输出的第一行显示了该对象的 toString() 方法返回的相同字符串,表示异常类名称,后面的行表示先前由方法 fillInStackTrace() 记录的数据。
句法:

public void printStackTrace()

返回值:此方法不返回任何内容。
下面的程序说明了Java.lang.Throwable 类的 printStackTrace 方法:
示例 1:

Java
// 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
// 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
// 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
// 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
// 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);
        }
    }
}


Java
// 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");
    }
}


输出:
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

// 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

// 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

// 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 以及指定打印 Writer 发生异常的类名和行号等其他详细信息。此方法与 printStackTrace() 的工作方式相同,但不同之处仅在于它打印到作为参数传递的指定打印 Writer。
句法:

public void printStackTrace(PrintWriter s)

参数:此方法接受PrintWriter作为参数,该参数指定要写入此 Throwable 详细信息的打印写入器。
返回值:此方法不返回任何内容。
下面的程序说明了 Throwable 类的 printStackTrace(PrintWriter s) 方法:
示例 1:

Java

// 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

// 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 Java()
https://docs.oracle.com/javase/10/docs/api/java Java Java)
https://docs.oracle.com/javase/10/docs/api/java Java Java)