Basic HTML version of Foils prepared Sept 6 1998

Foil 46 User Created Exceptions

From Java Tutorial 98-2: Java Language and Object Oriented Techniques CPS606 Fall Semester 1999 -- Sept 7 1999. by Geoffrey C. Fox, Nancy McCracken

Exception examples for Foil 46
Exception class has 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.



© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Mon Sep 6 1999