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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
        }
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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()");
    }
}

chevron_right


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



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.