- 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
What are the different ways to print an exception message in java?
An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.
Printing the Exception message
You can print the exception message in Java using one of the following methods which are inherited from Throwable class.
printStackTrace() − This method prints the backtrace to the standard error stream.
getMessage() − This method returns the detail message string of the current throwable object.
toString() − This message prints the short description of the current throwable object.
Example
import java.util.Scanner; public class PrintingExceptionMessage { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter first number: "); int a = sc.nextInt(); System.out.println("Enter second number: "); int b = sc.nextInt(); try { int c = a/b; System.out.println("The result is: "+c); } catch(ArithmeticException e) { System.out.println("Output of printStackTrace() method: "); e.printStackTrace(); System.out.println(" "); System.out.println("Output of getMessage() method: "); System.out.println(e.getMessage()); System.out.println(" "); System.out.println("Output of toString() method: "); System.out.println(e.toString()); } } }
Output
Enter first number: 10 Enter second number: 0 Output of printStackTrace() method: java.lang.ArithmeticException: / by zero Output of getMessage() method: / by zero Output of toString() method: java.lang.ArithmeticException: / by zero at PrintingExceptionMessage.main(PrintingExceptionMessage.java:11)
- Related Articles
- Different ways to print exception messages in Java
- What are the different ways to iterate over an array in Java?
- How to capture and print Python exception message?
- What are the different ways of copying an array into another array in Java?
- Different ways to create an object in java?
- Different ways to traverse an Array in Java?
- What are the different ways to install pandas?
- Print colored message with different fonts and sizes in C
- Print system time in C++ (3 different ways)
- What are the different ways of cooking eggs?
- Different ways to create Objects in Java
- Different ways to concatenate Strings in Java
- What are the different ways to select an option from a dropdown using Selenium Webdriver?
- How to print custom message instead of ErrorStackTrace in java?
- What are the different ways to include dry fruits in our diet?

Advertisements