Error handling in AppleScript uses try on error. The code which may throw an error goes in the try block and any error handling code is in the on error block. The on error block is closed using end try.
foo is not defined, so throws an error. When an error occurs, the dialog is displayed.
try
...