CS 683 Emerging Technologies: Embracing Change Spring Semester, 2001 Exceptions |
||
---|---|---|
© 2001, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 27-Feb-01 |
Exceptions
Basic Handling of Exceptions
Format
[ProtectedBlock] on: ExceptionList do: [:exception | HandlerBlock]
Example [1/0] on: ZeroDivide do: [:exception | Transcript show: exception description; cr]Unlike Java, in Smalltalk zero divide by both integer and floats cause a zero divide exception to be raised
Exceptions are Classes
Exception class is the parent of all exceptions
Subclasses define specialized exceptions
Important Subclasses
Raising Exceptions
Implicitly Raised Exceptions
Exceptions can be raised by VM
12 / 0
Explicitly Raised Exceptions
Send the message signal, signal: to an exception class
Warning signal: 'This string is the signal description'
Error signal
Exceptions & Return Values
on:do: is a message, so returns a value
If an exception is raised the return value is:
| result | result := [10/1] on: ZeroDivide do: [:exception | Float infinity ]. ^result
| result | result := [10/0] on: ZeroDivide do: [:exception | Float infinity ]. ^result
Catching Multiple Exceptions
Use a comma or ExceptionSets
[1/0] on: Warning , ZeroDivide do: [:exception | code here]
| exceptions | exceptions := ExceptionSet with: Warning with: ZeroDivide [1/0] on: exceptions do: [:exception | code here]
Inheritance and Exception
All subexceptions are caught by an exception in on:do:
ZeroDivide is a subclass of Error
The ZeroDivide exception will be caught in the following
[1/0] on: Error do: [:exception | Transcript show: exception description; cr]
Finding the Exception Handler
When an exception is raised
The enclosing handlers are searched
[[1/0] on: ZeroDivide do: [:exception | Transcript show: 'First']] on: ZeroDivide do: [:exception | Transcript show: 'Second']
Warning Default Action
Warning default action
Warning signal: 'Hi Mom'. Transcript show: 'End'
[Warning signal: 'Hi Mom'. Transcript show: 'End'] on: Warning do: [:exception | Transcript show: 'Handler']
Notification Default Action
Notification default action
Notification signal: 'Hi Mom'. Transcript show: 'End'
[Notification signal: 'Hi Mom'. Transcript show: 'End'] on: Exception do: [:exception | Transcript show: 'Handler']
What is the Default Action for Exception X?
Look at the defaultAction method in the exception's class
Resumable Exceptions
Some exceptions are resumable
| result | [result := 10/0. Transcript show: result.] on: ZeroDivide do: [:exception | exception resume: Float infinity ]
Output in Transcript infinity
Exception Messages that exit the Handler
resume or resume:
Example - resume:
10/0 raises an exception
The handler requests resumption with value 1
The expression 10/0 returns 1
The sum becomes 1 + 5
| result | [result := 10/0 + 5. Transcript show: result.] on: ZeroDivide do: [:exception | exception resume: 1 ]
Output in Transcript 6
Example - resume
10/0 raises an exception
The handler requests resumption, no value
The expression 10/0 returns nil
| result | [result := 10/0. Transcript show: result.] on: ZeroDivide do: [:exception | exception resume ]
Output in Transcript nil
Example - retry
x/y raises an exception
The handler sets y := 1.
The block is reexecuted
| x y result | x := 10. y := 0. [result := x / y. Transcript show: result.] on: ZeroDivide do: [:exception | y := 1. exception retry ]
Output in Transcript 10
Example - return
The following are equivalent
| result | [result := 10/0. Transcript show: result.] on: ZeroDivide do: [:exception | exception return]
| result | [result := 10/0. Transcript show: result.] on: ZeroDivide do: [:exception | nil]
Example - return: The following are equivalent
| result | result := [10/0] on: Error do: [:exception | Float infinity ]. ^result
| result | result := [10/0] on: Error do: [:exception | exception return: Float infinity ]. ^result
Some Error Handling Messages in Object
self assert: [block]
ensure:
Format
[block] ensure: [clean up block]Ensure that the clean up block will be done
If block ends due to an exception
[[10/0] ensure: [Transcript show: 'In ensure'; cr]] on: ZeroDivide do: [:exception | Transcript show: 'In handler';cr ]
Creating Your Own Exceptions
Subclass the correct existing Exception