Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between try catch and NSSetUncaughtExceptionHandler

I have tried two methods for catching the exceptions. First one is with a try catch and the second one is with the following code in Appdelegate.

void onUncaughtException(NSException* exception)
{
//save exception details
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 NSSetUncaughtExceptionHandler(&onUncaughtException);
}

The advantage of the second method is we don need to implement try catch blocks in each and every method.

The first one catches the exception, prints it but does not crash the application.. But the second one catches the exception and crashes the application.

Is there any way to use the second method to catch exceptions without crashing the application.

like image 243
Mano Avatar asked Mar 11 '14 04:03

Mano


1 Answers

NSSetUncaughtExceptionHandler Sets the top-level error-handling function where you can perform last-minute logging before the program terminates. in the onUncaughtException you can do something before crash, but app do crash finally.

@try...@catch...@finally.. is to try to catch possible NSException, if catch, run in the @catch block code, no matter if catch, code will run in @finally block code. Using @try...@catch... will not cause crash finally, this may be the main difference.

like image 84
simalone Avatar answered Oct 31 '22 05:10

simalone