1 |
Exception class has two constructors, one of which allows a message to be included in each instance.
|
2 |
The user can either throw an exception of type Exception with a unique message, or create own subclass of Exception:
|
3 |
public static void MyMethod() throws MyException
-
{ . . .
-
throw new MyException;
-
. . . }
|
4 |
class MyException extends Exception
-
{ public MyException ()
-
{ super ("This is my exception message."); }
-
}
|
5 |
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.
|