Re: Best way to programmatically cancel an NSError?
Re: Best way to programmatically cancel an NSError?
- Subject: Re: Best way to programmatically cancel an NSError?
- From: George Orthwein <email@hidden>
- Date: Mon, 24 Sep 2007 15:56:15 -0400
On Sep 24, 2007, at 12:30 PM, Matt Neuburg wrote:
I'm not heavily experienced at this, but I've done quite a bit of
using
NSErrors and I'm curious what "suppress the error" means. I'm
accustomed to
doing one of two things:
* If I catch an NSError arriving thru the responder chain, I can
create and
return a different NSError in willPresentError to substitute it as
it heads
up the chain.
* If I catch the situation earlier, in validateXXX, I can create an
NSError
and return NO, or just do whatever I like and return YES.
You seem to be talking about some further option, and I'm wondering
what it
is. Thx - m.
Yes, indeed. In the first case, it seems you can only swap in a
different error with willPresentError. I'd like to cancel the NSError
meaning that no alert will get shown to the user.
In your second example, you can fail to generate the error in
validateXXX, but can it be "stopped" once it is generated?
Your message prompted me to further investigate NSUserCancelledError.
From the Error Handling Guide:
Important: You should always special-case test for the
NSUserCancelledError error code (in the NSCocoaErrorDomain). This
code indicates that the user cancelled the operation (for example,
by pressing Command-period). In this case, you should not display
any error dialog.
http://developer.apple.com/documentation/Cocoa/Conceptual/
ErrorHandlingCocoa/ErrorRespondRecover/chapter_3_section_3.html
So apparently there is a way to "not display" the error once generated.
And found in Document-Based Applications Overview:
If you override such a method to prevent some action, but you don't
want an error alert to be presented to the user, return an error
object whose domain is NSCocoaErrorDomain and whose code is
NSUserCancelledError. The Application Kit presents errors through
the NSApplication implementations of the presentError: and
presentError:modalForWindow:delegate:didPresentSelector:contextInfo: m
ethods declared by NSResponder. Those implementations silently
ignore errors whose domain is NSCocoaErrorDomain and whose code is
NSUserCancelledError.
http://developer.apple.com/documentation/Cocoa/Conceptual/Documents/
Articles/ErrorHandling.html
So it seems there IS a standard way to programmatically "cancel"
errors. I can simply swap in an error with the above specs in
willPresentError: and it will not get shown to the user.
Thanks for prompting me to research further!
George
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden