Java中可丢弃的fillInStackTrace()方法

这个 fillInStackTrace() 方法 JAVA可丢弃的 类,在该可丢弃对象中记录有关当前线程堆栈帧的当前状态的信息。这意味着使用此方法可以看到类的当前方法的异常消息,其中调用了fillInStackTrace()方法。如果有其他消息可以从当前方法派生,其中抛出异常,那么可以跳过这些其他额外的消息细节。

null

语法:

public Throwable fillInStackTrace()

返回值: 此方法返回对应用fillInStackTrace()的可丢弃对象的引用。

下面的程序演示了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()" );
System.err.println( "Caught Inside Main:" );
e.printStackTrace();
// Exception details using fillInStackTrace()
System.out.println( "Exception details with fillInStackTrace()" );
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()时,该方法将返回异常详细信息,直到调用fillInStackTrace()方法的showResults方法。但是main()方法显示了整个异常细节,因为在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)

参考: https://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享