Navigation:  Programming Cookbook > Exception Handling >

Raising Exceptions

Previous pageReturn to chapter overviewNext page

Having detected that a particular exceptional condition has occurred, one needs to be able to instantiate and raise an instance of the appropriate Exception class. The easiest way to do this is by using the class #signal: method. For example:

Error signal: 'A gratuitious error'

 

The ANSI standard specifies that the argument to #signal: should implement the <string> protocol, but Dolphin will actually accept any object to which it sends #displayString in order to generate the error message text.

One can also include additional information in the form of a 'tag' that can be hold any type of Object. In order to set the tag one uses the class #signal:with: method, for example:

Win32Fault signal: 'Floating point stack under/overflow' with: aWin32ExceptionRecord

 

To set the tag alone without setting a specific error message one can use the class #signalWith: method.

If one needs to capture additional information when raising an exception, then the object can be instantiated in the normal way, and once set-up with the use of accessor methods, it can be sent #signal/signal:/signalWith: to actually raise it. For example BoundsError is an error class which includes the receiver whose bounds were violated, and the index at which the violating attempt was made. BoundsErrors are raised as follows:

^BoundsError new

  receiver: self;

  signalWith: anInteger

 

Bear in mind that one is free to add whatever behaviour and state one needs in one's own exception classes.