Different Ways to Print Exception Message in Java
In this section, we will learn how to use various Java Throwable class methods to print exception messages. The Throwable class has the three methods listed below for printing the exception message:
- Using the printStackTrace Method
- Employing the getMessage() Method
- utilizing the method toString()
Various Java Methods for Printing Exceptional Messages Let's go over each in more depth one by one.
Using the printStackTrace() method
The Throwable class, which is a part of the java.lang package, contains the definition of the printStackTrace() method. The method outputs an exception's title, description (such as "/ by zero," line number, and class name), and stack trace. The subsequent exception's location is shown on the stack trace. The exception message can be printed with great frequency.
The printStackTrace() method comes in three different iterations:
Syntax | Description |
printStackTrace() | That throwable as well as its backtrace are printed by the method toward the standard deviation stream. |
public void printStackTrace(PrintStream h) | The throwable as well as its backtrace are printed by the method to the designated print stream. |
public void printStackTrace(PrintWriter h) | The throwable as well as its backtrace are printed by the procedure to the designated print writer. |
We will first develop a Java program that produced a divide by zero exception in order to better grasp the notion behind the printStackTrace() method. The printStackTrace() method will not be used in this program to print the exception.
PrintExceptionMessageDemo1.java
public class PrintExceptionMessageDemo1
{
//the user defined method
public static void divide()
{
try
{
// raised exception for division by zero
int b = 100/0;
}
catch (Exception e)
{
//exception message is printed
System.out.println(e);
}
}
//it is the main() method
public static void main(String args[])
{
// the use of a user-defined method
divide();
}
}
The following message appears on the console when the aforementioned application is run, and we encounter an arithmetic exception:
Output:
java.lang.ArithmeticException: / by zero
We are unable to identify the line in the message that throws the exception. Thus, it is challenging to identify instances of exceptions. We utilise the printStackTrace() method to solve this issue. Let's use a Java programme to use the printStackTrace() method.
PrintExceptionMessageDemo2.java
public class PrintExceptionMessageDemo2.java
{
//It is the user defined method
public static void divide()
{
try
{
//it is raised divide by the zero exception
int b = 100/0;
}
catch (Exception e)
{
//displays the exception's message and specifics
e.printStackTrace();
}
}
//it is the main() method
public static void main(String args[])
{
// user defined method is called
divide();
}
}
Output:
Java.lang.ArithmeticException: / by zero
at PrintExceptionMessageDemo2.divide(PrintExceptionMessageDemo2.java:9)
at PrintExceptionMessageDemo2.main(PrintExceptionMessageDemo2.java:21)
The exception message mentioned above makes it apparent which procedure raised the error, what kind of error it was, and which line actually threw the error.
The application throws a java.lang, as can be seen in the message's first line.
ArithmeticException (division by zero) (divide by zero). The second line demonstrates that the procedure division() throws an exception and that exceptions start to occur at line 9. That exception is shown at line 21 of the third line. Because the split() method is invoked inside the main() method, the main() method similarly throws an exception. In light of this, we can quickly identify the precise location of the exception by using the printStackTrace() method.
Employing the getMessage() Method
The Throwable class, which is a part of the java.lang package, also contains the definition of the getMessage() method. Just the information of an exception is printed by the technique. Neither the exception's name nor its description is printed. The exception message can be printed with great frequency.
Syntax:
public String getMessage();
The throwable instance's detailed message string is returned via the function. It might be empty.
Let's use the Java program's getMessage() method.
public class PrintExceptionMesssageDemo3.java
{
public static void main(String args[])
{
try
{
int b = 100/0;
}
catch (Exception e)
{
//message of the exception is only printed
System.out.println(e.getMessage());
// Using the following statement will print the title of the exception and the exception that was thrown.
//System.out.println(e);
}
}
}
Output:
/ by zero
It merely prints the exception, as we can see. Due to the lack of a thorough description of an exception, it is not generally utilised.
Utilizing the method toString()
The throwable class's toString() method takes precedence over the Object class's toString() method. It outputs an exception's succinct description. The other details aren't displayed (like exception name and stack trace). The printing of exception messages is not very common.
Consider implementing the toString()method in a Java program.
public class PrintExceptionMessageDemo4
{
public static void main(String args[])
{
try
{
int b = 100/0;
}
catch (Exception e)
{
// The exception message can be printed using
// either of the statements; both print the same message.
System.out.println(e.toString()); //System.out.println(e);
}
}
}
Output:
Java.lang.ArithmeticException: / by zero
We can see from the message above that it just prints the exception's name and kind. It does not specify the line number where the exception occurred.
We have examined the various Java exception message printing methods. Because it identifies the place an error occurs, we advise using the printStackTrace() method.