What is the correct approach to handling exceptions in iOS?

I found out that Objective-C has an equivalent way of handling exceptions, such as C #, in .NET. Also, as the Apple docs say, I would like to handle / handle exceptions by creating an NSError object. We’ll take a closer look at the section “Capturing various types of exceptions” in the exception handling documentation .

.... I would like to catch different types of exceptions. In .NET, I use to view a class method document to get possible exceptions that it can raise. Where to get this information from apple documents? How do I know which exceptions can be caused by a-method / object / process?

thanks for your suggestions

Tom

+5
source share
3

Apple, . ( , NSArray.)

.NET . Cocoa . , .NET, , , - (, -). Cocoa , NSErrors. , .

, Cocoa , , . ( , , .NET, .)

+5

Objective-C, , , . , . NSError*:

NSErrror *error = nil;
BOOL success = [somebody doSomethingWithError:&error];
if (!success) {
    NSLog(@"Got error: %@", error);
}

:

- (BOOL) doSomethingWithError: (NSError**) error
{
    error = error ? error : &(NSError*){ nil };
    if (somethingWentWrong) {
        *error = [NSError …];
        return NO;
    }
    // All is fine
    return YES;
}

, . - (, [NSFileHandle writeData:]), , , , , , .

+5

. cocoa nilArgumentException, NSException. , :

if ([[exception name] isEqualToString:MyAppException]) 

, NSException.

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;

:

NSException, , .

+2

All Articles