views:

159

answers:

2

what is the syntax for handle the exceptions in iphone sdk? how to handle the exceptions in iphone. what documentation to know more about? tutorial, sample code are most wanted and thankful.

+2  A: 

See Apple's documentation on Exception handling in Objective-C:

Basic example from the docs:

Cup *cup = [[Cup alloc] init];

@try {
    [cup fill];
}
@catch (NSException *exception) {
    NSLog(@"main: Caught %@: %@", [exception name], [exception reason]);
}
@finally {
    [cup release];
}
gammelgul
+3  A: 

Exceptions in Objective-C are quite a contentious issue, even Apple themselves discourage you from using them unless absolutely necessary.

My first question would be what do you want to achieve from the exception handling? If you're looking from a Java perspective and how exceptions are so tightly integrated in that language for handling errors (i.e. flow control) then I think it's unadvisable to use objective-c exceptions for this purpose, you need to use NSError and handle errors that way.

This is a snippet from Apples documentation: -

Exceptions are resource-intensive in Objective-C. You should not use exceptions for general flow-control, or simply to signify errors. Instead you should use the return value of a method or function to indicate that an error has occurred, and provide information about the problem in an error object. For more information, see Error Handling Programming Guide For Cocoa.

djhworld
an interesting commentary, for sure. a major purpose of exceptions is to do just what apple says to not use them for. reading your greyed text, that's exactly why exceptions are useful.
KevinDTimm
"You should not use exceptions ... to signify errors". *Really*? In that case, I've been using exceptions incorrectly for a long time :P
Onion-Knight
Exceptions should be for exceptional conditions, things that should not occur, no matter what the user is doing. They can signify errors, but the errors should be very rare and application-ending. If you need to catch common errors (can't write to disk, network connection failed, etc.), and present them to the user, passing around an NSError is the recommended approach.
Brad Larson
@Brad - but isn't the definition of an error an exceptional condition? Note that even this answer says that this is a contentious issue, where (it would seem) Apple has deemed their definition of exception to be different from every one else -- see C++ and Java for a definition of everyone ;)
KevinDTimm
@KevinDTimm ...you think Apple would conform to everyone? Ha! Yeah I see what you mean, I guess Apple deem exceptions to be thrown in *exceptional* circumstances and thus should not be used control the flow of your application (for example, you can't write to a file because it doesn't exist etc)
djhworld
I'm an old school guy, one exit from functions, that kind of thing. Exceptions are a god-send, I can create a long list of things I want to do and have them throw exceptions when then fail. Since they aren't supposed to fail, this really makes things better. But, not 'permitting' this (or just strongly recommending against it) just seems a cop-out. Fix the language, fix the compiler, don't complain that it's too expensive.
KevinDTimm