exception handling in iphone?
what is the syntax 开发者_StackOverflow中文版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.
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.
See Apple's documentation on Exception handling in Objective-C:
- The Objective-C Programming Language
- Exceptions in Cocoa
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];
}
精彩评论