What are important methods of Java Exception Class?

Following are some important methods of Exception class:

  1. public Throwable getCause() – Returns the cause of this throwable or null if the cause is nonexistent or unknown. (The cause is the throwable that caused this throwable to get thrown.)
  2. public String getLocalizedMessage() – Creates a localized description of this throwable. Subclasses may override this method in order to produce a locale-specific message. For subclasses that do not override this method, the default implementation returns the same result as getMessage().
  3. public String getMessage() – Returns the detail message string of this throwable.
  4. public void printStackTrace() – Prints this throwable and its backtrace to the standard error stream. This method prints a stack trace for this Throwable object on the error output stream that is the value of the field System.err. The first line of output contains the result of the toString() method for this object. Remaining lines represent data previously recorded by the method fillInStackTrace().
  5. public String toString() – Returns a short description of this throwable. The result is the concatenation of:
    • the name of the class of this object
    • “: ” (a colon and a space)
    • the result of invoking this object’s getLocalizedMessage() method

If getLocalizedMessage returns null, then just the class name is returned.