Different Ways to Print Exception Message in JavaIn 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 TopicJava Copy Constructor Example |
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