Java Throwable setStackTrace()方法与示例

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值