Easy Tutorial
❮ Number Valueof Java Properties Class ❯

Java Example - Exception Handling Methods

Java Examples

The following example demonstrates the use of the System.err.println() method from the System class to showcase exception handling:

ExceptionDemo.java File

class ExceptionDemo {
    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();
        }
    }
}

The output of the above code is:

Caught Exception
getMessage():My Exception
getLocalizedMessage():My Exception
toString():java.lang.Exception: My Exception
printStackTrace():
java.lang.Exception: My Exception
   at ExceptionDemo.main(ExceptionDemo.java:5)

Java Examples

❮ Number Valueof Java Properties Class ❯