Google+

228. Printing the exception details in different ways









Pre-requisite -


As explained in my previous Post#220 Catching Exceptions using Throwable Class, we have used to the following statement in the catch block to print the occurred Arithmetic exception as shown below -


 catch(Throwable object1)
    {
          System.out.println("Exception Details - "+object1);
     }


The red color text in the above print statement can be divided into two parts -
  1. "Exception Details - " is the user defined message given by the program 
  2. object1 displays the system defined message on printing
+  operator is used to concatenate (i.e. combine) the user defined message with the system defined message. When an exception occurs (example - arithmetic divide by 0 exception), the print statement in the above catch block will get executed and the following message will get displayed in the output -

Output -

Exception Details - java.language.ArithmeticException: /by zero



Ways to print the exception details -

1. Print the User Message -

We can choose to print only the User defined message when an exception occurs in the try block by using the following print statement in the catch block -


 catch(Throwable object1)
    {
          System.out.println("Exception Occurred");   //Only prints the User defined message
     }


Output -

Exception Occurred

2. Print the System Defined Message -

We can choose to print only the System Defined Message when an exception occurs in the try block by using the following print statement in the catch block -


catch(Throwable object1)
    {
          System.out.println(object1);   //Only prints the System defined message
     }


Output -

java.language.ArithmeticException: /by zero

3. Print the User Defined and System Defined Message together -

We can choose to print the User Defined and System Defined Messages together by writing the following print statement in the catch block -


 catch(Throwable object1)
    {
          System.out.println("Exception Details - "+object1); //Prints User define and System defined messages
     }

Output -

Exception Details - java.language.ArithmeticException: /by zero

4. Other ways of printing the message -

We can also print the System Details in many ways by using the below methods of the Throwable Class.
   
  • getMessage( )
  • toSring( )
  • printStackTree( )

These methods need to be used with the exception (i..e. object) to print the System Defined Message in different ways. Lets implement these methods in the upcoming posts.

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a Java Class file 'PrintExceptions.java' with main( ) method in the existing Java Project 'Project 46' as shown below -



2. Write a statement which throws any exception say Arithmetic Exception in try block as shown below -


3. Create a catch block which throws the above thrown Arithmetic Exception as shown below -


4. Print the User defined message while handling an exception by writing the following statement in catch block as shown below -


5. Save and Run the Java Class 'PrintExceptions.java' and observe that the above thrown arithmetic exception is handled by printing the following in the output -


6. Now write another statement in the catch block to print the System define message by writing the following statement in the catch block as shown below -


7. Save and Run the Java Class 'PrintExceptions.java' and observe that the above thrown arithmetic exception is handled by printing the following in the output -



8. Now write another statement in the catch block to print the User defined and System define messages together by writing the following statement in the catch block as shown below -


9. Save and Run the Java Class 'PrintExceptions.java' and observe that the above thrown arithmetic exception is handled by printing the following in the output -








Please comment below to feedback or ask questions.

Using getMessage( ) of Throwable Class will be explained in the next post.











No comments: