
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
Print Stack Trace in Java
In order to print the stack trace in Java, we use the java.lang.Throwable.printStackTrace() method. The printStackTrace() method prints the throwable and its backtrace in the standard error stream.
Declaration - The java.lang.Throwable.printStackTrace() method is declared as follows −
public void printStackTrace()
Let us see a program to print the stack trace in Java.
Example
public class Example { public static void main(String args[]) throws Throwable { try { int n = 3; System.out.println(n/0); } catch (ArithmeticException e) { System.out.println("Printing stack trace..."); e.printStackTrace(); // prints the stack trace } } }
Output
Printing stack trace... java.lang.ArithmeticException: / by zero at Example.main(Example.java:8)
Advertisements