Re: Proper way to throw an exception in an iOS application...
Re: Proper way to throw an exception in an iOS application...
- Subject: Re: Proper way to throw an exception in an iOS application...
- From: Fritz Anderson <email@hidden>
- Date: Sat, 17 Mar 2012 15:41:30 -0500
On 17 Mar 2012, at 3:19 PM, Brian Lambert wrote:
> One camp says to use:
> [NSException raise:NSInvalidArgumentException format:@"pageIndex is out of range"];
>
> Which I was first exposed to and have been using.
>
> This doesn't seem to be ideal, though, because the compiler doesn't recognize that this represents a throw and sometimes you wind up with the awkward: "Control may reach end of non-void function" case and have to code something like:
>
> [NSException raise:NSInternalInconsistencyException format:@"Mumblefart can't be somethingorothered"];
> return nil; // Can't actually get here. Awkward Code...
>
> It would seem, then, that using @throw would be superior:
>
> @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"pageIndex is out of range" userInfo:nil];
>
> But it doesn't seem to be highly used in code I'm reading.
> I'm leaning towards switching to @throw.
>
> How do the awesomest engineers do this? Your thoughts?
I assume you've already heard the talk about the awesomest engineers' not usually throwing exceptions at all. Exceptional conditions are logic errors to be prosecuted out of existence before the product ships.
That being the case, I almost always use the NSAssert() and NSParameterAssert() macros. They cover exactly the cases you show here, they do the testing without a visible if () block, and they become no-ops when you define NS_BLOCK_ASSERTIONS in your release build. Because assertions encapsulate the fail branch, you'd have to provide a return value. I wouldn't lose sleep over it.
— F
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden