带有示例的Java中的可抛出 setStackTrace() 方法
Throwable 类的setStackTrace(StackTraceElement[] stackTrace)方法用于将堆栈跟踪元素设置为该可抛出对象,该堆栈跟踪将由 getStackTrace() 返回并由 printStackTrace() 和相关方法打印。此方法允许用户覆盖默认堆栈跟踪,该堆栈跟踪要么在构造 throwable 时由 fillInStackTrace() 生成,要么在从序列化流中读取 throwable 时反序列化。
如果任何 Throwable 的堆栈跟踪不可写,则调用此方法除了验证其参数外没有任何效果。
句法:
public void
setStackTrace(StackTraceElement[] stackTrace)
参数:此方法只接受一个参数stackTrace ,它是与此 Throwable 关联的堆栈跟踪元素。
返回:此方法不返回任何内容。
下面的程序说明了 setStackTrace() 方法:
示例 1:
Java
// 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;
}
}
Java
// 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;
}
}
输出:
ClassNameOfExe.methodNameOfExe(fileNameOfExe:10)
示例 2:
Java
// 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)
参考:
https://docs.oracle.com/javase/10/docs/api/java Java Java%5B%5D)