
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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 Articles
- What are the differences between paint() method and repaint() method in Java?
- What are the differences between get() and navigate() method?
- Differences between wait() and sleep() method in 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 is the difference between method hiding and method overriding in Java?
- What is the difference between method overloading and method hiding in Java?
- What are the differences between C++ and Java?
- What are the differences between C and Java?
- What is difference between a Java method and native method
- What is the difference between a Java method and a native method?
- What are the differences between Java classes and Java objects?
- What are the differences between JRadioButton and JCheckBox in Java?
- What are the differences between recursion and iteration in Java?

Advertisements