Section 14.4. Isolating and Handling Errors


14.4. Isolating and Handling Errors

Although logs (Section 14.2.1) are a great way to track a script's progress, they suffer from one major flaw: you can't correct code errors with them. Sure, you can always go back to your scriptonce you've read its logand try to make your corrections by hand. But that's a hit-and-miss endeavor; if your script continues misbehaving, you have to continue tweaking it over and over again until you fix the problem.

Suppose you had the following script, for example:

tell application "Finder"     close Finder window "Macintosh HD" end tell

Now, if you had a Finder window open to Macintosh HD when you ran that script, everything would work just fine. The trouble comes if you don't have a Finder window open to Macintosh HD. In that case, since AppleScript can't locate a matching Finder window, you get the unhelpful dialog box shown in Figure 14-6.

Figure 14-6. When AppleScript encounters an error, not only does it display a dialog box, it also stops the progress of your entire script. Not fun.


14.4.1. Using try

Luckily, AppleScript provides a special statement for isolating such errors: try. When you surround a section of code in a try statement, you're telling AppleScript, "run this code, but if there's an error with it, just ignore it and carry on." To make your previous script error-proof, therefore, you'd make these changes:

tell application "Finder"     try         close Finder window "Macintosh HD"     end try end tell

When you run that script, everything works as it should. If there's a Finder window named Macintosh HD, the script closes it; if there isn't a matching Finder window, your script simply remains silent. That way, you get error handling and a functioning script, all in one.

14.4.1.1 Multiple commands in a try statement

On the other hand, if you use a try statement that surrounds more than one line of code, AppleScript will jump to the end of the try statement if an error occurs. That means that in the following script:

try     display dialog ("hello" as number)     display dialog "goodbye" end try tell application "Finder" to activate

AppleScript never gets to your second display dialog command, because there's an error with the first. But AppleScript will get to your activate command, because it's outside of the error-afflicted try statement.

14.4.2. Discovering Errors

Unfortunately, even using a try statement isn't perfect. Sure, your script will continue to run without showing any errors, but you'll never know if a problem actually occurred.

That's why an error statement is so useful: it lets you not only isolate bugs in your script but also run a special section of code if a bug occurs. For example, the following script will silently log a message to your Event Log if an error occurs:

tell application "Finder"     try         close Finder window "Macintosh HD"     on error         log "An error occurred while your script was running."     end try end tell

Now when an error occurs, the script jumps to the error statement and runs your log command. You get both error protection (with try) and error notification (with on error), all without bringing your script to a halt.

If you'd like to know the specific error that stopped your try statement, simply append a variable to your error handler, like this:

tell application "Finder" try close Finder window "Macintosh HD" on error theMessage  --The error message is now held in theMessage  log "This error occurred:" & theMessage end try end tell

For more error statement tricks, visit http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/AppleScript.c4.html.



AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net