DekGenius.com
Team LiB   Previous Section   Next Section
try [on error] [number | from | partial result | to] end[error | try]

Syntax

Try
   (* code statements here *)
   on error errText
   display dialog "An error:" & errText
end try

Description

try represents AppleScript's all-important error-trapping capability. If any of the statements that are enclosed in a try...end try statement block raise an error, then AppleScript catches the error and prevents it from taking down the whole script. After try catches the error (similar to Java's try...catch exception-trapping syntax), the script has the option of adding inside the try block the reserved words on error followed by any code that should execute in response to the error.

on error is optional inside of try statements beginning with AppleScript 1.4.

The program will then resume following the end try part of the try block, as though nothing happened. Without a try block, AppleScript's default error behavior is to display an error message in a dialog box then cancel the running script. try only catches one error at a time. By using the on error statement and its numerous parameters, you can uncover all kinds of details about the error, but you do not have to use it. In the OS versions previous to Mac OS 9, Script Editor does not compile a script that includes a try block without an on error statement.

Examples

This example traps any errors caused by invalid data entered by the user, and then goes on its merry way without explicitly responding to any errors. try statements can be used inside and outside of your own subroutines, script objects, and libraries; they can nest other statements such as if, repeat, and tell. In fact, your entire script can run inside of a try statement, and the try block can contain other try statements:

try
   set userReply to the text returned of¬
      (display dialog "Try your best to enter a number." default answer¬ 
      "")
   set invalidNum to false
   set userReply to userReply as real
on error
   set invalidNum to true
end try
if invalidNum then
   display dialog "That's the best you can do?!"
else
   display dialog "thanks for entering: " & userReply
end if

This script politely asks the user for a number; it sets the reply to the variable userReply. This variable is then coerced from a string to a real type, which raises an error if userReply is not a valid number. For example, "a10" couldn't be converted to a valid number. AppleScript displays this error and stops running the script if we do not catch it in the try block. If the error is raised, the statements that appear between on error and end try execute. In this case, the script sets a boolean variable invalidNum to true. Remember, the script does not have to use the on error statement part of try in Mac OS 9 or OS X. It can simply use a try block to prevent any errors from crashing the script, then go on blithely executing the rest of the code. The error handler of the try statement contains five variables from which you can obtain information about any errors. The following code shows two of the many ways that you can use try. The first demonstration catches but then skips over any errors that might be raised while it executes its code. The second use of try deploys the on error handler to grab all the data that it can about the error and display it to the user:

tell application "SoundJam™ MP"
   try
      activate (* will raise an error if SoundJam isn't on the computer, 
      but the program will just keep going *)
   end try
   try
      set allPlay to playlist windows -- a list of playlists
      repeat with pl in allPlay
         if (name of pl) is "tranceControl" then set mainPlay to pl
      end repeat
      set trackNameList to name of (tracks of mainPlay)
      set trackMsg to ""
      on error errMsg number errNum from objErr partial result errList¬
      to  errClass 
      (* display the error message, error number, the object that is the 
      source of the error, any partial results, and class information *)
         display dialog errMsg & ": " & errNum & return & "Source of¬ 
         error   was: " & objErr & return & "Here are any partial¬ 
         results: " &  errList & return & "If coercion failure it¬
         involved a coercion to: " & errClass
     return -- exit the program
   end try
   repeat with nam in trackNameList
      set trackMsg to trackMsg & return & nam
   end repeat
   display dialog "The MP3 track names in the main playlist are: " &¬ 
   return & trackMsg
end tell

In the prior example, if any statements in the second try block raise an error, then the on error handler displays error information using all five parameters of on error. AppleScript gives these parameters a value (e.g., the error description and number) for you if any errors are raised. The values for the partial list and to parameters are empty lists if there are no partial results or coercion problems associated with the error. Here's a rundown of the five optional on error parameters:

  • The first nonlabeled parameter is a string describing the error, as in on error errMsg. The variable errMsg, which you create, contains the error message.

  • The number parameter contains the error number, as in on error number errNum. Use the number label followed by your own variable to contain the number.

  • The object that was the source of the error is labeled with the keyword from. An example is on error from objErr. You create the variable following the reserved word from, and if AppleScript can identify the object source of the error, it will store the name of the object in that variable.

  • If the error-causing operation involved getting a list of values, and it was successful in getting some of the list values, then this list is stored in the variable labeled with the reserved words partial list. The content of this variable is of type list.

  • If the error was caused by a faulty coercion, than the class that the script failed to coerce some value to is identified in the variable following the reserved word to, as in on error to errClass. The identifier errClass contains the word describing the class, such as boolean, list, or real.

    Team LiB   Previous Section   Next Section