- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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)
- Related Articles
- Place Stack Trace into a String in Java
- How to get stack trace using thread in Java 9?
- Java Program to Convert a Stack Trace to a String
- How to rethrow InnerException without losing stack trace in C#?
- How can I get a stack trace for a JavaScript exception?
- How to print different stack frames using StackWalker API in Java?\n
- Stack in Java
- How can I get a JavaScript stack trace when I throw an exception?
- Print Reverse a linked list using Stack
- Stack in Java Programming
- Explanation about SAP ABAP Stack and JAVA Stack and role of Java Stack during ECC upgrade
- Run-time Stack mechanism in Java
- Trace or track Python statement execution (trace)
- Which is in More Demand: Java Full Stack or Python Full Stack
- Add an element to a Stack in Java

Advertisements