Throwable fillInStackTrace() method in Java
The fillInStackTrace() method, of java.lang.Throwable class, records within this Throwable object information about the current state of the stack frames for the current thread. It means using this method one can see the Exception messages of the current method of a class, in which fillInStackTrace() method is called. If there are other messages which can be derived from the current method, in which exception is thrown, then one can skip these other extra message details. Syntax:
public Throwable fillInStackTrace()
Return Value: This method returns a reference to this Throwable object on which fillInStackTrace() is applied. Below programs illustrates fillInStackTrace() method of Method class: Program 1: This Program shows what results print if one doesn’t use fillInStackTrace() method and what happens if one uses fillInStackTrace() method Explanation: Using fillInStackTrace() only returns information of the active state of frames for the current thread. So when fillInStackTrace() is called, then the method returns details up to main method in which fillInStackTrace() method was called.
Java
public class GFG {
public static void main(String[] args) throws Throwable
{
GFG gfg = new GFG();
try {
gfg.method();
}
catch (Exception e) {
System.out.println("Exception details without fillInStackTrace()\n");
System.err.println("Caught Inside Main:");
e.printStackTrace();
System.out.println("Exception details with fillInStackTrace()\n");
System.err.println("Caught Inside Main:");
e.fillInStackTrace();
e.printStackTrace();
}
}
public void method() throws Throwable
{
divide();
}
void divide()
{
try {
System.out.println( 10 / 0 );
}
catch (ArithmeticException e) {
throw e;
}
}
}
|
Output:
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)
Program 2: This Program print details after applying fillInStackTrace(). Explanation: Using fillInStackTrace() only returns information of the active state of frames for the current thread. So when fillInStackTrace() is called, then the method returns exception details up to showResults method in which fillInStackTrace() method was called. But the main() method is showing whole exception details because fillInStackTrace() was not called in main method.
Java
public class GFG {
public static void main(String[] args) throws Throwable
{
GFG gfg = new GFG();
try {
gfg.showResults();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void showResults() throws Throwable
{
try {
exceptionThrownMethod();
}
catch (Exception e) {
e.printStackTrace();
throw e.fillInStackTrace();
}
}
public void exceptionThrownMethod() throws Exception
{
throw new Exception(" this is thrown from function1()");
}
}
|
Output:
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)
Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html
Last Updated :
28 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...