Re: Cocoa / OC exception handling
Re: Cocoa / OC exception handling
- Subject: Re: Cocoa / OC exception handling
- From: "Kyle Sluder" <email@hidden>
- Date: Fri, 17 Oct 2008 19:41:34 -0400
On Fri, Oct 17, 2008 at 7:12 PM, Dale Miller <email@hidden> wrote:
> In particular, the examples show a series of @catch blocks arranged in
> most-specific to least-specific order. Since the different blocks in the
> example
> have different parameter specifications for the different blocks this seems
> to imply that there is some sort of filtering of which block to invoke on an
> exception.
The exception will be handled by the first @catch block that matches
the type of the exception.
@try {
@throw [[[MyCustomException alloc] init] autorelease];
}
@catch(MyCustomException *e) {
// This will execute.
}
@catch(NSException *e) {
// This will not.
}
However, pay careful attention to the word "first". This means that
if the first @catch block in scope handles exceptions of some really
generic type, like NSException or id, then that will execute. In
fact, if the compiler sees that you've put more specific exception
handler after a more generic one, it will issue a warning that the
more specific @catch block will not execute:
@try {
@throw [[[MyCustomException alloc] init] autorelease];
}
@catch(NSException *e)
{
// This will execute.
}
@catch(MyCustomException *e) {
// This will not. The compiler generates
// a warning here to let you know.
}
> Does falling off the end of a
> @catch block invoke the next @catch block ( or the @finally if there are no
> more @catch blocks)?
No. Read above. At most one @catch block will execute. If none
executes, the exception mechanism looks farther up the call stack to
find a matching @catch block; if it gets all the way to the bottom of
the stack and no matching @catch is found, then the generic "Uncaught
exception" handler is invoked, and your program dies.
If a @catch block somewhere executes, then all the @finally blocks
that should execute are executed in the order of the stack's
unwinding, and then control resumes after the @try/@catch/@finally
block that caught the exception. It's just like exception handling in
any other language.
--Kyle Sluder
_______________________________________________
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