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)
相关用法
- Java Throwable toString()用法及代码示例
- Java Throwable getCause()用法及代码示例
- Java Throwable getStackTrace()用法及代码示例
- Java Throwable initCause()用法及代码示例
- Java Throwable addSuppressed()用法及代码示例
- Java Throwable printStackTrace()用法及代码示例
- Java Throwable getSuppressed()用法及代码示例
- Java Throwable getMessage()用法及代码示例
- Java Throwable getLocalizedMessage()用法及代码示例
- Java Throwable fillInStackTrace()用法及代码示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代码示例
- Java Java lang.Long.highestOneBit()用法及代码示例
- Java Java lang.Long.byteValue()用法及代码示例
- Java Java.util.Collections.rotate()用法及代码示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Throwable setStackTrace() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。