In this section, you will learn how to obtain the stack trace.
When you want to generate an exception in a program, it will be better to print out the exception stack trace to catch the error instead of using e.getMessage() method. By the stack trace, you will know which method threw an exception and how that method is called. The method printStackTrace() method prints a stack trace from the exception and provides more information about the error process.
In the given example, we are trying to read the non-existent file 'data.txt' file by using the method dis.readLine(). Therefore, FileNotFoundException occurs.
Here is the code of GetStackTrace.java
Output will be displayed as
Liked it! Share this Tutorial