
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
When to Use fillInStackTrace Method in Java
The fillInStackTrace() is an important method of Throwable class in Java. The stack trace can be useful to determine where exactly the exception is thrown. There may be some situations where we need to rethrow an exception and find out where it is rethrown, we can use the fillInStackTrace() method in such scenarios.
Syntax
public Throwable fillInStackTrace()
Example
public class FillInStackTraceTest { public static void method1() throws Exception { throw new Exception("This is thrown from method1()"); } public static void method2() throws Throwable { try { method1(); } catch(Exception e) { System.err.println("Inside method2():"); e.printStackTrace(); throw e.fillInStackTrace(); // calling fillInStackTrace() method } } public static void main(String[] args) throws Throwable { try { method2(); } catch (Exception e) { System.err.println("Caught Inside Main method()"); e.printStackTrace(); } } }
Output
Inside method2(): java.lang.Exception: This is thrown from method1() at FillInStackTraceTest.method1(FillInStackTraceTest.java:3) at FillInStackTraceTest.method2(FillInStackTraceTest.java:7) at FillInStackTraceTest.main(FillInStackTraceTest.java:16) Caught Inside Main method() java.lang.Exception: This is thrown from method1() at FillInStackTraceTest.method2(FillInStackTraceTest.java:11) at FillInStackTraceTest.main(FillInStackTraceTest.java:16)
Advertisements