Java Examples - Exception Methods


Advertisements


Problem Description

How to handle the exception methods ?

Solution

This example shows how to handle the exception methods by using System.err.println() method of System class.

public class NewClass {
   public static void main(String[] args) {
      try {
         throw new Exception("My Exception");
      } catch (Exception e) {
         System.err.println("Caught Exception");
         System.err.println("getMessage():" + e.getMessage());
         System.err.println("getLocalizedMessage():" + e.getLocalizedMessage());
         System.err.println("toString():" + e);
         System.err.println("printStackTrace():");
         e.printStackTrace();
      }
   }
}

Result

The above code sample will produce the following result.

Caught Exception
getMassage(): My Exception
gatLocalisedMessage(): My Exception
toString():java.lang.Exception: My Exception
print StackTrace():
   java.lang.Exception: My Exception
   at ExceptionMethods.main(ExceptionMeyhods.java:12)

java_exceptions.htm

Advertisements