3 Different ways to print Exception messages in Java
In Java, there are three methods to print an exception information. All of them are present in Throwable class. Since Throwable is the base class for all exceptions and errors, so we can use these three methods on any exception object.
- java.lang.Throwable.printStackTrace() method : By using this method, we will get name(e.g. java.lang.ArithmeticException) and description(e.g. / by zero) of an exception separated by colon, and stack trace (where in the code, that exception has occurred) in the next line.
Syntax:public void printStackTrace()
// Java program to demonstrate// printStackTrace methodpublicclassTest{publicstaticvoidmain(String[] args){try{inta =20/0;}catch(Exception e){// printStackTrace method// prints line numbers + call stacke.printStackTrace();// Prints what exception has been thrownSystem.out.println(e);}}}chevron_rightfilter_noneRuntime Exception:
java.lang.ArithmeticException: / by zero at Test.main(Test.java:9)Output:
java.lang.ArithmeticException: / by zero
- toString() method : By using this method, we will only get name and description of an exception. Note that this method is overridden in Throwable class.
// Java program to demonstrate// toString methodpublicclassTest{publicstaticvoidmain(String[] args){try{inta =20/0;}catch(Exception e){// toString methodSystem.out.println(e.toString());// OR// System.out.println(e);}}}chevron_rightfilter_noneOutput:
java.lang.ArithmeticException: / by zero
- java.lang.Throwable.getMessage() method : By using this method, we will only get description of an exception.
Syntax:public String getMessage()
// Java program to demonstrate// getMessage methodpublicclassTest{publicstaticvoidmain(String[] args){try{inta =20/0;}catch(Exception e){// getMessage method// Prints only the message of exception// and not the name of exceptionSystem.out.println(e.getMessage());// Prints what exception has been thrown// System.out.println(e);}}}chevron_rightfilter_noneOutput:
/ by zero
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Exception Propagation in Java
- Comparison of Exception Handling in C++ and Java
- Java | Exception Handling | Question 8
- Java | Exception Handling | Question 1
- Types of Exception in Java with Examples
- Java | Exception Handling | Question 7
- Java | Exception Handling | Question 8
- Understanding OutOfMemoryError Exception in Java
- Java | Exception Handling | Question 4
- Java | Exception Handling | Question 2
- Java | Exception Handling | Question 3
- Infinity or Exception in Java when divide by 0?
- Java | Exception Handling | Question 6
- Null Pointer Exception In Java
- Nested try blocks in Exception Handling in Java



