Reputation: 213
I learned that Objective-C has an equivalent way of handling exceptions like C# does in .NET. Furthermore as the apple docs says, I'd like to handle/process exceptions, creating an NSError-object. Taking a close look at the section "Catching Different Types of Exception" in the docs exception handling
....I would like to catch different types of exception. In .NET I am used to browse the doc of a class-method to get the possible exceptions it can raise. Where to get this information from the apple-docs? How do I know, what kind of exceptions a -method/object/process- can raise?
Upvotes: 5
Views: 4930
Reputation: 104065
Error handling in the Objective-C world is probably quite different from what you are used to. To put it shortly, forget about exceptions. Most errors are handled by return values or by passing a pointer to NSError*
:
NSErrror *error = nil;
BOOL success = [somebody doSomethingWithError:&error];
if (!success) {
NSLog(@"Got error: %@", error);
}
And on the callee side:
- (BOOL) doSomethingWithError: (NSError**) error
{
error = error ? error : &(NSError*){ nil };
if (somethingWentWrong) {
*error = [NSError …];
return NO;
}
// All is fine
return YES;
}
This looks cumbersome, but in practice it mostly works fine. In the rare occasion that something really can throw an exception (like [NSFileHandle writeData:]), the documentation mentions the fact, but I don’t think you are expected to analyze the exception as much as customary in other languages.
Upvotes: 5
Reputation: 10251
Look at the developer reference for exception handling. In cocoa we don't get exceptions like nilArgumentException
, We get only NSException
. To give fine grained message or handling you can do the following,
if ([[exception name] isEqualToString:MyAppException])
Below is the List of Exception names defined in the NSException header file.
FOUNDATION_EXPORT NSString * const NSGenericException;
FOUNDATION_EXPORT NSString * const NSRangeException;
FOUNDATION_EXPORT NSString * const NSInvalidArgumentException;
FOUNDATION_EXPORT NSString * const NSInternalInconsistencyException;
FOUNDATION_EXPORT NSString * const NSMallocException;
FOUNDATION_EXPORT NSString * const NSObjectInaccessibleException;
FOUNDATION_EXPORT NSString * const NSObjectNotAvailableException;
FOUNDATION_EXPORT NSString * const NSDestinationInvalidException;
FOUNDATION_EXPORT NSString * const NSPortTimeoutException;
FOUNDATION_EXPORT NSString * const NSInvalidSendPortException;
FOUNDATION_EXPORT NSString * const NSInvalidReceivePortException;
FOUNDATION_EXPORT NSString * const NSPortSendException;
FOUNDATION_EXPORT NSString * const NSPortReceiveException;
FOUNDATION_EXPORT NSString * const NSOldStyleException;
CORRECTION :
You can subclass NSException class,as suggested in one of the comments below, to catch custom exception.
Upvotes: 2
Reputation: 7605
As Apple's documentation says, most exceptions are thrown in exceptional circumstances. (Some exceptions aren't, like accessing an object out of NSArray bounds.)
.NET encourages local exception handling. Cocoa is written to encourage large-scope exception handling. The reason you have local exception handling in .NET is that you expect some part to fail in an expected way (like a network error when downloading something). In Cocoa, this is handled by using methods that return NSErrors instead. It's the same thing, only more visible in the method signature.
A good rule of thumb is that Cocoa only throws an exception in situations where it's unclear how you would even recover. (Don't mistake this for exceptions being thrown all over the place like in .NET and being this difficult to handle.)
Upvotes: 5