I’m new to ASObjC and would like to ask a very basic question: What is the recommended way (or ways) of handling errors in ASObjC?
In AppleScript, I would typically catch an error in a try block. A common use would be to capture the user-canceled error (number -128) of a display dialog window as shown in the following trivial example:
try
display dialog "Set n to 1 or 2 ?" buttons {"2", "1"} cancel button "2"
set n to 1
on error
set n to 2
end try
Is this considered an acceptable construct in ASObjC, or are other approaches preferred, perhaps utilizing NSError or something else?
Browser: Safari 536.26.17
Operating System: Mac OS X (10.8)
You still use try for AS errors. If a Cocoa method includes an NSError argument, when there is an error it usually returns missing value (nil), and you can get more info from the NSError. The method description should tell you what is returned in the case of an error.
It’s reassuring that try blocks constitute acceptable usage in ASObjC given their central role in AppleScript, and I look forward to learning more about NSError. Thank you for the very helpful information.
You can keep using try, but you do have to test whether a result is missing value quite often. The NSError can be a key to why it happened, a bit like the error number or error message in a try.