Throwable类setStackTrace()方法 (Throwable Class setStackTrace() method)
setStackTrace() Method is available in java.lang package.
setStackTrace()方法在java.lang包中可用。
setStackTrace() Method is used to sets stack trace elements that will be retrieved by using getStackTrace() method.
setStackTrace()方法用于设置将通过使用getStackTrace()方法检索的堆栈跟踪元素。
setStackTrace() Method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
setStackTrace()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
setStackTrace() Method may throw an exception at the time of setting stack trace elements.
setStackTrace()方法在设置堆栈跟踪元素时可能会引发异常。
NullPointerException – This exception may throw when the given argument is null or when any of its existing elements of the stack trace is null.
NullPointerException-当给定参数为null或堆栈跟踪的任何现有元素为null时,可能引发此异常。
Syntax:
句法:
public void setStackTrace(StackTraceElement[] st_tr);
Parameter(s):
参数:
StackTraceElement[] st_tr – represents an array of "StackTraceElement".
StackTraceElement [] st_tr –表示“ StackTraceElement”的数组。
Return value:
返回值:
The return type of the method is void, it returns nothing.
该方法的返回类型为void ,不返回任何内容。
Example:
例:
// Java program to demonstrate the example
// of void setStackTrace(StackTraceElement[] st_tr)
// method of Throwable
public class SetStackTrace {
public static void main(String args[]) {
try {
stackTraceMethod();
} catch (Throwable ex) {
// By using getStackTrace() method is to get the
// stack trace element
StackTraceElement[] st_tr = ex.getStackTrace();
System.err.println("st_tr[0].toString()" + st_tr[0].toString());
}
}
public static void stackTraceMethod() throws Throwable {
int li_nu = 7;
// instantiate a new exception called
// Throwable
Throwable th = new Throwable("Raise New Exception");
StackTraceElement[] st_tr = new StackTraceElement[] {
new StackTraceElement("cl_na", "me_na", "fu_na", li_nu)
};
// By using setStackTrace() method is to set
// the elements in stack
th.setStackTrace(st_tr);
throw th;
}
}
Output
输出量
st_tr[0].toString()cl_na.me_na(fu_na:7)
翻译自: https://www.includehelp.com/java/throwable-setstacktrace-method-with-example.aspx