开发者

How to catch NSInvalidArgumentException?

is there any way to开发者_Go百科 catch it? Or is this a bug?


So to answer this question, I used the following:

@try{ 
   //your code
} @catch (NSException* exception) {
   NSLog(@"Got exception: %@    Reason: %@", exception.name, exception.reason);
}

and the string NSInvalidArgumentException is printed for 'exception.name' so clearly one could just test that exception.name for the actual exception type.

Not the prettiest, but it works.


If I understand you correctly, you ask why you can't catch the NSInvalidArgumentsException. To be precise, the exception is not caught even if you try to catch it as NSException, which should always work. I also experienced this problem and after looking around - this issue indeed looks like an Apple's bug. Please see this page. People report there that the issue exists only on simulators and works correctly on real devices.


As @David mentioned we can use below code

@try{ 
   //your code
} @catch (NSException* exception) {
   NSLog(@"Got exception: %@    Reason: %@", exception.name, exception.reason);
}

and @Moshe Kravchik mentioned, there situations were some exceptions are not caught , that is mainly when we dispatches new block in a queue , so I would recommend to add separate try catch block for each queue dispatches.

ie, try..catch in main() will not catch the exception happened in AppDelegate class, so need to add separate try..catch in AppDelegate

eg:

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    @try{

        self.viewController = [[MainViewController alloc] init];

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            @try{
                NSLog(@"This is a block");
                NSObject * b= nil;
                [NSArray arrayWithObject:b];
            } @catch (NSException* exception) {
                NSLog(@"Got exception: %@    Reason: %@", exception.name, exception.reason);
            }
        });

        return [super application:application didFinishLaunchingWithOptions:launchOptions];
    } @catch (NSException* exception) {
        NSLog(@"Got exception: %@    Reason: %@", exception.name, exception.reason);
    }
}

for any uncaught exception, we should use NSSetUncaughtExceptionHandler, that will not stop application from crashing.

void onUncaughtException(NSException* exception)
{
    NSLog(@"onUncaughtException : %@", exception.reason);
}

int main(int argc, char* argv[])
{
    @autoreleasepool {
        NSSetUncaughtExceptionHandler(&onUncaughtException);
        int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
         return retVal;
    }
}

for signals other than exception , like bad memory usages etc we should listen for particular signal

void signalHandler(int signal)
{
    NSLog(@"Got signal %d",signal);
}

    signal(SIGPIPE, &signalHandler);
    signal(SIGABRT, &signalHandler );
    signal(SIGHUP, &signalHandler );
    signal(SIGINT, &signalHandler );
    signal(SIGQUIT, &signalHandler );
    signal(SIGILL, &signalHandler );
    signal(SIGIOT, &signalHandler );
    signal(SIGFPE, &signalHandler );
    signal(SIGSEGV, &signalHandler );
    signal(SIGSYS, &signalHandler );
    signal(SIGPIPE, &signalHandler );

More details about uncaught exception here.


It is a bug. Specifically, a bug in your code. NSInvalidArgumentException means you've passed bad data into a method. Frequently this means you've passed nil as an argument that doesn't allow nil. The exception description should provide more information as to what method/argument is bad.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜