User Created Exceptions
The Exception class has data structures and methods that can give information about the exception and how it occurred. There are two constructors, one of which allows a message to be included in each instance.
The user can either throw an exception of type Exception with a unique message, or create own subclass of Exception:
- public static void MyMethod() throws MyException
- { . . .
- throw new MyException;
- . . . }
- class MyException extends Exception
- { public MyException ()
- { super ("This is my exception message."); }
- }
Methods which call "MyMethod" should use a try and catch block which catches an exception e of type MyException. Methods e.getMessage and e.printStackTrace can be used on Exceptions.