Next: , Previous: , Up: Exception handling  

6.11.5 Creating new exception classes

If you want code to be able to handle your signalled exceptions, you will probably want to provide a way to pick those kinds out automatically. The easiest way to do this is to subclass Exception.

First, you should choose an exception class to specialize. Error is the best choice for non-resumable exceptions, and Notification or its subclass Warning is best for exceptions that should resume with nil by default.

Exceptions are just normal objects; include whatever information you think would be useful to handlers. Note that there are two textual description fields, a description and a message text. The description, if provided, should be a more-or-less constant string answered from a override method on #description, meant to describe all instances of your exception class. The message text is meant to be provided at the point of signalling, and should be used for any extra information that code might want to provide. Your signalling code can provide the messageText by using #signal: instead of #signal. This is yet another reason why signalling variants of instance creation messages can be more trouble than they’re worth.