Javatpoint Logo
Javatpoint Logo

Different Ways to Print Exception Message in Java

In this section, we will learn how to print exception messages in Java by using different methods of the Java Throwable class. The Throwable class provides the following three methods to print the exception message:

  • Using printStackTrace Method
  • Using getMessage() Method
  • Using toString() Method
Different Ways to Print Exception Message in Java

Let's discuss one by one in detail.

Using printStackTrace() Method

The printStackTrace() method is defined in the Throwable class that belongs to java.lang package. The method prints the name, description (such as / by zero), and the stack trace (line number and class name where exception raised) of an exception. The stack trace traces where the next exception occurs. It is widely used to print the exception message.

There are three versions of the printStackTrace() method:

Syntax Description
printStackTrace() The method prints this throwable and its backtrace to the standard error stream.
public void printStackTrace(PrintStream s) The method prints throwable and its backtrace to the specified print stream.
public void printStackTrace(PrintWriter s) The method prints throwable and its backtrace to the specified print writer.

To understand the concept of the printStackTrace() method, first, we will create a Java program that raised a divide by zero exception. In this program, we will not use the printStackTrace() method to print the exception.

PrintExceptionMessage1.java

When we run the above program, we get an arithmetic exception and the following message prints on the console:

Different Ways to Print Exception Message in Java

In the above message, we cannot point out which line throws an exception. So, it is difficult to find where exceptions occur. To overcome this problem we use the printStackTrace() method. Let's use the printStackTrace() method in a Java program.

PrintExceptionMessage2.java

Let's run the above program.

Different Ways to Print Exception Message in Java

The above exception message clearly shows which method has raised an exception, which type of exception is, and which line throws an exception.

The first line of the message shows that the program throws a java.lang.ArithmeticException (divide by zero). The second line shows that exceptions occur at line 9 and the method divide() throws an exception. The third line shows that exception at line 21. The main() method also throws an exception because the divide() method is called inside the main() method. Hence, using the printStackTrace() method, we can easily point out the exact location of the exception.

Using getMessage() Method

The getMessage() method is also defined in the Throwable class that belongs to java.lang package. The method prints only the message of an exception. It neither prints the name of the exception nor the description. It is widely used to print the exception message.

Syntax:

It returns the detail message string of this Throwable instance. It may be null.

Let's use the getMessage() method in a Java program.

PrintExceptionMesssage3.java

Let's run the above program.

Different Ways to Print Exception Message in Java

We observe that it prints only the exception. So, it is not widely used because it does not print the detailed description of an exception.

Using toString() Method

The toString() method of the Throwable class overrides the toString() method of the Object class. It prints the short description of an exception. It does not show the other information (like exception name and stack trace). It is not widely used to print the exception message.

Let's use the toString() method in a Java program.

PrintExceptionMessage4.java

Let's run the above program.

Different Ways to Print Exception Message in Java

In the above message, we observe that it prints only the name and type of the exception. It does not point out at which line exception has occurred.

We have seen the different ways to print the exception message in Java. We suggest you that use the printStackTrace() method because it points to the location where an exception occurs.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA