Language Guide

CHAPTER 7
Control Statements
Try Statements 209
on error errText number errNum
display dialog "An error has occurred: " &
errText & "\rDo you want to continue " &
"using the default file?"
buttons {"Cancel", "Continue"} default button 1
if button returned of result = "Cancel"
error number -128 --quit silently
else
display dialog "The script will continue " &
"using the default file."
set fileName to defaultFileName
end if
end try
For the preceding example to work correctly, defaultFileName must have
been set to a filename earlier in the same script.
The next example demonstrates the use of the To keyword to capture addi-
tional information about an error that occurs during a coercion failure.
tell application "Scriptable Text Editor"
try
repeat with i from 1 to "Toronto"
i
end repeat
on error from obj to newClass
{obj, newClass}
end try
end tell
--result: {"Toronto", integer}
The Repeat statement fails because the string "Toronto" is the wrong class.
The error handler simply returns the values of obj (the offending value,
"Toronto") and newClass (the class of the coercion that failed, integer) in
the result window.