by Dinesh Thakur

The Throwable class provides the following commonly used methods.

String getMessage (): It returns the description of the exception. It includes fully qualified name of the Exception class and a brief description of the exception.

•  void printStackTrace(): Prints the Throwable object and its stack trace information on the console.

String toString (): It returns the String object containing a description of the exception. This description includes the full name of the Exception class followed by comma and a space and the string returned from the getMessage () method.

StackTraceElement [] getStackTrace(): It returns an array that contains the stack trace, one element at a time as an array of stack trace elements. The method at the top of the stack is the last method called before the exception was thrown.

void printStackTrace (PrintStream s): This is same as printStackTrace () method except that you specify the output stream as an argument.

Throwable fillInStackTrace (): It returns a Throwable object that contains a completed stack trace. This object can be rethrown.

The following program demonstrates the use of Throwable methods to display exception information when an exception is thrown.

public class ThrowableMethods

{
       static void A()
       {
         int x = 12 ;
         int y = 0 ;
         int z = x/y;
       }
       public static void main(String[] args)
       {
            try
            {
                A();
             }
             catch(ArithmeticException e)
            {
                    System.out.println("Displaying stack trace information...");
                    e.printStackTrace();
                    System.out.println("\nDescription of the exception : "+e.getMessage());
                    System.out.println("\nString containong Description of the exception");
                    System.out.println(e.toString());
             }
        }
}

Throwable Class Example in Java