You can also watch it on YouTube:
Today we'll look at error handling in VA Smalltalk - the ANS exception system, and the older, instance based signal mechanism. First, let's look at a few simple examples, using divide by zero as the source of our error:
"simple exception handling" [10 / 0] on: ZeroDivide do: [:ex | Transcript cr. Transcript show: ('Error: <', ex printString, '>'); cr].
When the code in the block throws the exception we are looking for, the handle block executes, printing to the Transcript. You can query the exception for details as well:
"simple description" [10 / 0] on: ZeroDivide do: [:ex | Transcript cr. Transcript show: ('Error: <', ex description, '>'); cr].
You can also resume the exception (assuming it's resumable - you need to look at the implementation of #isResumable for the exception in question):
"just resume" [10 / 0] on: ZeroDivide do: [:ex | ex resume]. Transcript show: 'Resumed'; cr.
That will continue past the exception as if nothing happened. Probably not the best handling in most circumstances, but it's something you can do which comes in handy on occasion. One thing to be aware of in VA Smalltalk; not all of the ANSI spec for exceptions has been implemented. #retryUsing: does not work in VA:
"retry - not implemented" [10 / 0] on: ZeroDivide do: [:ex | ex retryUsing: [10 / 2]]
Will yield this exception:
This will only come up if you are porting code from another Smalltalk (Pharo or VW, for instance) - when writing native VA code, this isn't really a problem. The older Signal (instance based) exception mechanism still exists in VA, and is heavily used in the libraries. Using that looks like this:
"Signal based" [10 / 0] when: ExError do: [:signal | signal exitWith: nil]
For the most part, handlers using #when:do: will look like that, although you can, of course, customize the do block.
Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.
[st4u150-iPhone.m4v ( Size: 6548434 )]