Java Throwable fillInStackTrace() Method



Description

The Java Throwable fillInStackTrace() method fills in the execution stack trace. This method records within this Throwable object information about the current state of the stack frames for the current thread.

Declaration

Following is the declaration for java.lang.Throwable.fillInStackTrace() method

public Throwable fillInStackTrace()

Parameters

NA

Return Value

This method returns a reference to this Throwable instance.

Exception

NA

Example: Printing Stacktrace of Exception

The following example shows the usage of Java Throwable fillInStackTrace() method. We've defined two methods function1() which throws an Exception and function2() which call function1() and handles the exception and throw a reference of exception handled using fillInStackTrace() method. In main method, function2() method is called and in catch block exception stack trace is retrieved and printed.

package com.tutorialspoint;

public class ThrowableDemo {

   public static void function1() throws Exception {
      throw new Exception("this is thrown from function1()");
   }

   public static void function2() throws Throwable {
      try {
         function1();
      } catch(Exception e) {
         System.err.println("Inside function2():");
         e.printStackTrace();
         throw e.fillInStackTrace();
      }
   }

   public static void main(String[] args) throws Throwable {
      try {
         function2();
      } catch (Exception e) {
         System.err.println("Caught Inside Main:");
         e.printStackTrace();
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Inside function2():
java.lang.Exception: this is thrown from function1()
at ThrowableDemo.function1(ThrowableDemo.java:4)
at ThrowableDemo.function2(ThrowableDemo.java:9)
at ThrowableDemo.main(ThrowableDemo.java:19)
Caught Inside Main:
java.lang.Exception: this is thrown from function1()
at ThrowableDemo.function2(ThrowableDemo.java:13)
at ThrowableDemo.main(ThrowableDemo.java:19)

Example: Printing Stacktrace of Throwable

The following example shows the usage of Java Throwable fillInStackTrace() method. We've defined a method raiseException() which throws a Throwable after setting the Stacktrace. In main method, raiseException() method is called and in catch block exception stack trace is retrieved and printed using fillInStackTrace() method.

package com.tutorialspoint;

public class ThrowableDemo {

   public static void main(String[] args) {

      try {
         raiseException();
      } catch(Throwable e) {
         // prints stacktrace for this Throwable Object
         throw e.fillInStackTrace();
      }
   }
  
   public static void raiseException() throws Throwable {

      Throwable t = new Throwable("This is new Exception...");
      StackTraceElement[] trace = new StackTraceElement[] {
         new StackTraceElement("ClassName","methodName","fileName",5)
      };

      // sets the stack trace elements
      t.setStackTrace(trace);
      throw t;
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Exception in thread "main" java.lang.Throwable: This is new Exception...
	at com.tutorialspoint.ThrowableDemo.main(ThrowableDemo.java:11)

Example: Printing Stacktrace of Exception

The following example shows the usage of Java Throwable fillInStackTrace() method. We've defined a method raiseException() which throws an Exception after setting the Stacktrace. In main method, raiseException() method is called and in catch block exception stack trace is retrieved and printed using fillInStackTrace() method.

package com.tutorialspoint;

public class ThrowableDemo {

   public static void main(String[] args) {

      try {
         raiseException();
      } catch(Throwable e) {
         // prints stacktrace for this Throwable Object
         throw e.fillInStackTrace();
      }
   }
  
   public static void raiseException() throws Exception {

      Exception t = new Exception("This is new Exception...");
      StackTraceElement[] trace = new StackTraceElement[] {
         new StackTraceElement("ClassName","methodName","fileName",5)
      };

      // sets the stack trace elements
      t.setStackTrace(trace);
      throw t;
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Exception in thread "main" java.lang.Exception: This is new Exception...
	at com.tutorialspoint.ThrowableDemo.main(ThrowableDemo.java:11)
java_lang_throwable.htm
Advertisements