

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
What are the differences between printStackTrace() method and getMessage() method in Java?
There are two ways to find the details of the exception, one is the printStackTrace() method and another is the getMessage() method.
printStackTrace() method
- This is the method which is defined in java.lang.Throwable class and it is inherited into java.lang.Error class and java.lang.Exception class.
- This method will display the name of the exception and nature of the message and line number where an exception has occurred.
Example
public class PrintStackTraceMethod { public static void main(String[] args) { try { int a[]= new int[5]; a[5]=20; } catch (Exception e) { e.printStackTrace(); } } }
Output
java.lang.ArrayIndexOutOfBoundsException: 5 at PrintStackTraceMethod.main(PrintStackTraceMethod.java:5)
getMessage() method
- This is a method which is defined in java.lang.Throwable class and it is inherited into java.lang.Error and java.lang.Exception classes.
- This method will display the only exception message.
Example
public class GetMessageMethod { public static void main(String[] args) { try { int x=1/0; } catch (Exception e) { System.out.println(e.getMessage()); } } }
Output
/ by zero
- Related Questions & Answers
- What are the differences between paint() method and repaint() method in Java?
- What are the differences between get() and navigate() method?
- Differences between == and equals() method in Java
- Differences between wait() and sleep() method in Java?
- What are the differences between C++ and Java?
- What are the differences between C and Java?
- Differences between Lambda Expression and Method Reference in Java?
- Differences between Method Reference and Constructor Reference in Java?
- What is the difference between java method and native method?
- What are the differences between Java classes and Java objects?
- What are the differences between StackOverflowError and OutOfMemoryError in Java?
- What are the differences between ClassNotFoundException and NoClassDefFoundError in Java?
- What are the differences between recursion and iteration in Java?
- What are the differences between GridLayout and GridBagLayout in Java?
- What are the differences between JFrame and JDialog in Java?
Advertisements