The ‘Ark’ release adds support for raising and handling Exceptions–for those times when the seas get rough and you need a boat to get you safely through “exceptional” conditions 🙂 The release also includes fixes for a few bugs that were discovered as a side effect of introducing Exceptions.
Complete documentation of the new features and capabilities will be forthcoming over the next few days, but here’s a brief summary, with examples:
To ensure that critical code runs no matter what, put your business logic in a zero-argument block, and run it by sending the message #ensure: with a zero-argument block as the message argument. The second block will be run when the first block finishes, or if the first block is aborted by an exception:
[self processFiles] ensure: [self closeFiles]
To make sure that a block of code runs if and only if the main logic path is aborted by an exception (“curtailed,”) put the main logic path in a zero-argument block and evaluate it by sending the message #ifCurtailed:, with a zero-argument block as the message argument. The second block will be run if and only if the first block is aborted by an exception:
[self doBusinessLogic] ifCurtailed: [self handleFailure]
#on: ExceptionClass do: oneArgBlock
To protect your business logic with an exception handler, put it in a zero-argument block and send the block the message #on:do: with an Exception class as the first message argument and a one-argument block as the second message argument. If an exception is raised during the processing of the block that is sent the #on:do: message, and if sending the message #handles: theException to the ExceptionClass evaluates to true, then the oneArgBlockwill be evaluated with the raised exception as its argument:
[self doNormalApplicationActivity] on: MessageNotUnderstood do: [:exception | exception addTo: EventLog; notifyUser. self ensureGracefulShutdown]
#signal and #signal: messageText
To signal (raise, throw) an exception, send either the message #signal (with no argument) or #signal: aString to the class of the exception you would like to raise:
Warning signal: ‘Tsunami detected offshore!’
And that’s the end of the executive summary. Much more documentation will be published about Exceptions over the next few days.
The new release is available from the Essence# site on CodePlex.