Send to Printer

st4u

ST 4U 366: Errors and Exceptions in VA Smalltalk

April 5, 2013 11:08:53.575

Today's Smalltalk 4 You looks at the difference between exception handling and error handling in VA Smalltalk. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

error handling.

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:


In most Smalltalk code, you look for exceptions when there's a chance that your code will fail - typically, when dealing with something like file handling. In VA Smalltalk, there are two patterns in use - exceptions, and errors. For some of the system libraries (networking code, for example), you mostly won't see exceptions on failure; you'll get an error object back instead. You need to know what to look for in order to deal with the situation properly. The common Smalltalk pattern might look like this:


"Catch exception"
input := self getValueFromUser.
[10/input]
  on: ZeroDivide
  do: [:ex | ex return: 1].


Here we have code that might get a divide by zero error, and we handle that when it happens. If you use some of the VA libraries, here's the alternative error pattern:


"look for error"
client := (SstHttpClient forTransportScheme: 'http') startUp.
response := client get: 'http://www.5678jnbf.com'.
response isSstError
  ifTrue: [Transcript show: 'Error'; cr].
client shutDown.

Here, we don't get an exception when trying to get a nonsense url (in practice, you would be getting a proper url, but looking out for server errors, socket errors, etc). Here we need to check the response object - is it an actual response, or an error object? This is more akin to the pattern you see in other languages, where you need to check the return value for error codes.

The important thing to keep in mind is that both exist in VA, and you simply need to know which one to look for with the library you are using

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.

Tags: , , ,

Enclosures:
[st4u366-iPhone.m4v ( Size: 1079481 )]

posted by James Robertson

 Share Tweet This