Different Ways to Print Exception Message in Java7 Apr 2025 | 4 min read 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:
![]() Let's discuss one by one in detail. Using printStackTrace() MethodThe 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:
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: ![]() 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. ![]() 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() MethodThe 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. ![]() 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() MethodThe 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. ![]() 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. Next TopicCopy-constructor-in-java |
The Java ArrayList class is essentially a resizable array, indicating that its size can change dynamically based on the entries we add or remove. It can be found in the java.util package. The syntax listed below makes it simple to give an ArrayList as an argument...
3 min read
? In this section, we will learn different approaches to convert bytes to hexadecimal in Java. Convert Bytes to Hex There are the following ways to convert bytes to hexadecimal: Using Integer.toHexString() Method Using String.format() Method Using Byte Operation Using Integer.toHexString() Method It is a built-in function of the java.lang.Integer class. Syntax: public static String toHexString(int...
3 min read
The concept of delaying the loading of an object until one needs it is known as lazy loading. In other words, it is the process of delaying the process of instantiating the class until required. Lazy loading is important in a scenario where the cost of...
4 min read
Every local variable and the final blank field will have an assigned value when any value is accessed. Access to the value will consist of the variable's name or an area that occurs in an expression, except in the left-hand operand of the assignment operator, "=". To...
15 min read
In Java, BiFunction is a functional interface. It was introduced in Java 8. It can be used as an assignment target for a lambda expression or method reference. It belongs to java.util.function package. @FunctionalInterface public interface BiFunction<T,U,R> The interface accepts three type-parameters are as follows: T: It denotes the first...
2 min read
Working with floating-point numbers such as doubles is a common practice in Java. While comparing integers in Java, sometimes be a bit difficult due to their inherent errors. Comparison of integers in Java, is straightforward, compared with double values that requires careful consideration and accounting for...
6 min read
What is an ArrayList? In Java, ArrayList is a resizable array implementation. ArrayList expands dynamically, ensuring that there is always room for more elements to be added. An array of the Object class serves as the ArrayList's underpinning data structure. In Java, there are three constructors for...
4 min read
A javax.naming.CompositeName contains the getAll() function. The entire composite object's component set is returned as an enumeration of strings using the CompositeName class. Update effects on this enumeration that are applied to this composite name are not defined. Syntax: public Enumeration getAll() Parameters: The method...
2 min read
Java Multithreading is an essential feature that allows developers to write programs that can run concurrently on multiple threads. It helps developers to create responsive applications and improve the performance of the software. Many books have been written on this topic, providing in-depth knowledge of multithreading...
4 min read
An array of arrays can be a two-dimensional array. The 2D array is composed of matrices that show a set of rows and columns. We can access individual cells in a 2D array using their indices, just like we can with one-dimensional arrays, because the elements...
8 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India