java throwable用法_Java Throwable fillInStackTrace()用法及代码示例

java.lang.Throwable类的fillInStackTrace()方法在此Throwable对象中记录有关当前线程的堆栈帧的当前状态的信息。这意味着使用此方法可以看到类的当前方法的异常消息,其中调用了fillInStackTrace()方法。如果还有其他消息可以从当前方法派生而抛出异常,则可以跳过这些其他消息详细信息。

用法:

public Throwable fillInStackTrace()

返回值:此方法返回对其上应用了fillInStackTrace()的Throwable对象的引用。

以下示例程序旨在说明Method类的fillInStackTrace()方法:

示例1:该程序显示如果不使用fillInStackTrace()方法将显示什么结果,如果不使用fillInStackTrace()方法将发生什么结果

说明:使用fillInStackTrace()仅返回当前线程的帧的活动状态信息。因此,当调用fillInStackTrace()时,该方法将详细信息返回到调用fillInStackTrace()方法的主方法。

// Java program to demonstrate

// fillInStackTrace() method

public class GFG {

// Main Method

public static void main(String[] args) throws Throwable

{

GFG gfg = new GFG();

try {

// calling this method will throw exception

gfg.method();

}

catch (Exception e) {

// Exception details without using fillInStackTrace()

System.out.println("Exception details without fillInStackTrace()\n");

System.err.println("Caught Inside Main:");

e.printStackTrace();

// Exception details using fillInStackTrace()

System.out.println("Exception details with fillInStackTrace()\n");

System.err.println("Caught Inside Main:");

e.fillInStackTrace();

e.printStackTrace();

}

}

// method calling divide operation

public void method() throws Throwable

{

divide();

}

// divide operation throws ArithmeticException exception

void divide()

{

try {

System.out.println(10 / 0);

}

catch (ArithmeticException e) {

throw e;

}

}

}

输出:

Exception details without fillInStackTrace()

Caught Inside Main:

java.lang.ArithmeticException: / by zero

at GFG.divide(GFG.java:38)

at GFG.method(GFG.java:31)

at GFG.main(GFG.java:13)

Exception details with fillInStackTrace()

Caught Inside Main:

java.lang.ArithmeticException: / by zero

at GFG.main(GFG.java:23)

示例2:应用fillInStackTrace()后,该程序将打印详细信息。

说明:使用fillInStackTrace()仅返回当前线程的帧的活动状态信息。因此,当调用fillInStackTrace()时,该方法返回异常详细信息,直到showResults方法为止,在该方法中调用了fillInStackTrace()方法。但是main()方法显示了整个异常的详细信息,因为在主方法中未调用fillInStackTrace()。

// Java program to demonstrate

// fillInStackTrace() method

public class GFG {

// Main Method

public static void main(String[] args) throws Throwable

{

GFG gfg = new GFG();

try {

// calling this method will throw an exception

gfg.showResults();

}

catch (Exception e) {

// Exception details using fillInStackTrace()

e.printStackTrace();

}

}

// method calling exceptionThrownMethod()

// and when method returns Exception

// it is calling fillInStackTrace() method

public void showResults() throws Throwable

{

try {

exceptionThrownMethod();

}

catch (Exception e) {

e.printStackTrace();

throw e.fillInStackTrace();

}

}

// method throwing exception

public void exceptionThrownMethod() throws Exception

{

throw new Exception("this is thrown from function1()");

}

}

输出:

java.lang.Exception: this is thrown from function1()

at GFG.exceptionThrownMethod(GFG.java:35)

at GFG.showResults(GFG.java:27)

at GFG.main(GFG.java:13)

java.lang.Exception: this is thrown from function1()

at GFG.showResults(GFG.java:30)

at GFG.main(GFG.java:13)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值