@try{}@catch (NSException *exception) {}Applications and problems in object c

Everyone found out that there is no exception: in the java code often see exception capture, to avoid application exceptions and locate problems, give error prompts, and rarely seen in Apple development @try{}@catch (NSException *exception) { }. If the Apple code catches an anomaly and is in advanced cancer, it will still crash. Even if you don't add @try{}@catch (NSException *exception) {}, it's added, and it crashes. It is really a chicken function! Be careful. But it's really useless, and it doesn't happen. When I was doing the driver of Cao Cao, I met a driver who found us and said that his mobile phone was installed on the driver's side of Cao Cao. I have no problem testing my phone! My positioning is likely to be related to mobile phones. Let him find me, connect his mobile phone to XCODE, debug online, and find that the application dies on this code when it starts:

[_locationManager requestAlwaysAuthorization];

His mobile phone is iPhone 4s and the operating system is 8.0. I quickly hurry to locate it. I used to crash this code on iOS7:

            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Please authorize Cao Cao to use GPS location service in the setup!"message:nil delegate:self cancelButtonTitle:@"Settings"otherButtonTitles:nil, nil];
            alertView.tag =111;
//            @try
//            {
                [alertView show];
//            }
//            @catch (NSException *exception) {
//            }

I immediately added this exception capture to it, and the result was run again, ok. It can be seen that it is not all waste.     / / Request user authorization if there is no authorization     AppDelegate.m file:

    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)
    {
        @try
        {
            if (IOS_VERSION_LARGE_OR_EQUAL(8.0))
            {
                [_locationManager requestAlwaysAuthorization];
            }
        }
        @catch (NSException *exception) {
        }

    }

It can be seen that there is no need to add an exception to the popup.

For the file operation, according to the common writing method I used to write Vc, the error is captured on the read file, and the result is an illegal file path. It crashes directly and removes the exception. Although it reads a nil, it does not. As for the crash! Therefore, it is necessary to be careful to capture the anomaly. It is not possible to generalize, and it is better not to do anything that is superfluous!

+ (BOOL)storageImageWithFilePath:(NSString *)filePath
                           image:(UIImage *)image
{
    if(!filePath || ![filePath isKindOfClass:[NSString class]])
    {
        return NO;
    }
    if(!image || ![image isKindOfClass:[UIImage class]])
    {
        return NO;
    }
    //    NSFileManager* fm = [NSFileManager defaultManager];
    //    if([fm  fileExistsAtPath:filePath])
    {
        @try {
            NSData *imageData = UIImagePNGRepresentation(image);
            [imageData writeToFile:filePath atomically:YES];
            return YES;
        } @catch (NSException *exception) {
            NSLog(@"ImageCache save image error:%@", exception);
            return NO;
        } @finally {

        }
    }

}