➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
Different Ways to Get Exception Message in Java | In the Throwable class of Java, three methods are given to get the exception message from catch blocks. Throwable class is the superclass of all exceptions.
The three methods of Throwable class to print exception object stack trace are:-
1) printStackTrace()
2) toString()
3) getMessage()
These methods are useful when we write catch block() with superclass as a parameter. But before going further first we have to observe how JVM displays the exception message and how much the above-said methods display similar exception messages in Java.
Exception Messages displayed by the JVM
By default, the JVM displays the name of the thread, exception name, the reason for the exception, and the line/place of the exception where it occurs.
The exception message format, displayed by JVM:-
Exception in thread <thread-name> <exception-name> : <reason for the exception> <place of the exception>
Example to demonstrate how JVM display an exception message,
public class Test {
public static void main(String[] args) {
System.out.println(9/0);
}
}
The output of the above program:-
Exception in thread “main” java.lang.ArithmeticException: / by zero
at Test.main(Test.java:3)
Get Exception Message in Java Using printStackTrace()
The printStackTrace() method of java.lang.Throwable print exception name, the reason for the exception, and line/place of the exception where it occurs. It doesn’t display the thread name where the exception occurs.
The prototype of the printStackTrace() method is:-
- public void printStackTrace()
The exception message format, displayed by printStackTrace():-
Exception name: <reason of the exception> <place of the exception>
Example of displaying Exception message using printStackTrace() method,
catch(Exception e) {
e.printStackTrace();
}
Let us demonstrate it through an example,
public class Test {
public static void main(String[] args) {
try {
System.out.println(9/0);
} catch(ArithmeticException ae) {
ae.printStackTrace();
}
}
}
The output of the above program:-
java.lang.ArithmeticException: / by zero
at Test.main(Test.java:4)
Get Exception Message in Java Using toString()
The toString() method is an overridden method in the java.lang.Throwable class, its return type is String. It returns the exception name, and reason of the exception as a string.
The exception message format, displayed by toString():-
Exception name: <reason of the exception>
To display the exception message using the toString() method, we can call the toString() method or we can directly use the parameter name.
Example of displaying Exception message using toString() method,
catch(ArithmeticException e) {
System.out.println(e.toString());
// Or,
System.out.println(e);
}
Let us demonstrate it through an example,
public class Test {
public static void main(String[] args) {
try {
System.out.println(9/0);
} catch(ArithmeticException ae) {
System.out.println(ae);
}
}
}
The output of the above program:-
java.lang.ArithmeticException: / by zero
Get Exception Message in Java Using getMessage()
The getMessage() method of java.lang.Throwable class returns String. It gives only the reason for the exception. Most of the time we use the getMessage() method to display the message of the exception.
The exception message format, displayed by getMessage():-<reason for the exception>
Example to get exception message in Java using toString() method,
catch(Exception e) {
System.out.println(e.getMessage());
}
Let us demonstrate it through an Example,
public class Test {
public static void main(String[] args) {
try {
System.out.println(9/0);
} catch(ArithmeticException ae) {
System.out.println(ae.getMessage());
}
}
}
The output of the above program:-
/ by zero
Difference Between printStackTrace(), toString() and getMessage() Method
Way to get Message | Thread Name | Exception name | Reason for Exception | Place of Exception |
---|---|---|---|---|
JVM | Yes | Yes | Yes | Yes |
printStackTrace() | No | Yes | Yes | Yes |
toString() | No | Yes | Yes | No |
getMessage() | No | No | Yes | No |
Program to Demonstrate Different Ways to Get Exception Message in Java
class DisplayExceptionMsg {
public static void main(String[] args) {
try {
System.out.println(9/0);
} catch(ArithmeticException e) {
// getMessage() method
System.out.println("The getMessage() method Output: ");
System.out.println(e.getMessage());
System.out.println();
// toString() method
System.out.println("The toString() method Output: ");
System.out.println(e.toString());
// Or,
System.out.println(e);
System.out.println();
// printStackTrace()
System.out.println("The printStackTrace() method Output: ");
e.printStackTrace();
System.out.println();
// JVM default message
System.out.println("JVM default exception message: ");
throw e;
/* By using above statement we are
* re-throwing the caught exception.
*
* This exception will be caught
* by JVM default handler.
*/
}
}
}
The output of the above program:-
The getMessage() method Output:
/ by zero
The toString() method Output:
java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
The printStackTrace() method Output:
java.lang.ArithmeticException: / by zero
at DisplayExceptionMsg.main(Test.java:6)
JVM default exception message:
Exception in thread “main” java.lang.ArithmeticException: / by zero
at DisplayExceptionMsg.main(Test.java:6)
Excercise
Question) From the below options find which exception message is displayed by the JVM or by which method of java.lang.Throwable class?
Msg1) java.lang.ArithmeticException: / by zero
at FindExecutionFlow.main(ExceptionPropagation.java:15)
Msg2) Exception in thread “main” java.lang.ArithmeticException: / by zero
at FindExecutionFlow.main(ExceptionPropagation.java:15)
Answer:- The exception message of Msg1 is displayed by the prinStackTrace() and the second exception message is displayed by the JVM. The main difference in the exception message displayed by JVM and printStackTrace() is thread-name. The JVM displayed the name of the thread where the exception occurs but the printStackTrace() method doesn’t display thread-name.
Related Java topics
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!