为什么可以';我抓到这个iOS AWS SDK网络错误了吗?

为什么可以';我抓到这个iOS AWS SDK网络错误了吗?,ios,objective-c,amazon-web-services,try-catch,Ios,Objective C,Amazon Web Services,Try Catch,我正在我的应用程序中使用,最近我测试了一个没有网络的情况。这是我的代码: dispatch_queue_t Q = dispatch_queue_create("ec2_instance_fetch", NULL); dispatch_async(Q, ^{ AmazonEC2Client *client = [[AmazonEC2Client alloc] initWithAccessKey:[ESCredentialsManager accessKey] withSecretKey:

我正在我的应用程序中使用,最近我测试了一个没有网络的情况。这是我的代码:

dispatch_queue_t Q = dispatch_queue_create("ec2_instance_fetch", NULL);
dispatch_async(Q, ^{
    AmazonEC2Client *client = [[AmazonEC2Client alloc] initWithAccessKey:[ESCredentialsManager accessKey] withSecretKey:[ESCredentialsManager secretKey]];
    client.endpoint = [NSString stringWithFormat:@"https://%@", [ESRegionManager endpointForRegion:[ESRegionManager activeRegionObject]]];
    EC2DescribeInstancesResponse *response = [client describeInstances:[[EC2DescribeInstancesRequest alloc] init]];
    NSMutableArray *temp = @[].mutableCopy;
    for (EC2Reservation *reserv in response.reservations) {
        for (EC2Instance *instance in reserv.instances) {
            [temp addObject:instance];
        }
    }
    self.instances = temp;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
});
在无连接的情况下,它会抛出以下错误:

 *** Terminating app due to uncaught exception 'AmazonClientException', reason: 'Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo=0x16e55460 {NSErrorFailingURLStringKey=https://ec2.us-east-1.amazonaws.com/, NSErrorFailingURLKey=https://ec2.us-east-1.amazonaws.com/, NSLocalizedDescription=The Internet connection appears to be offline., NSUnderlyingError=0x16dea2c0 "The Internet connection appears to be offline."}'
*** First throw call stack:
(0x301adf4b 0x3a91c6af 0xbbae1 0xbb31d 0xba6c7 0xba177 0xbd3ab 0xa2bab 0x3adffd1b 0x3ae06273 0x3ae0606b 0x3ae06ce1 0x3ae06f59 0x3af41dbf 0x3af41c84)
libc++abi.dylib: terminating with uncaught exception of type AmazonServiceException
(lldb) 
因此,我试图简单地捕捉如下错误:

dispatch_queue_t Q = dispatch_queue_create("ec2_instance_fetch", NULL);
dispatch_async(Q, ^{
    @try {
        AmazonEC2Client *client = [[AmazonEC2Client alloc] initWithAccessKey:[ESCredentialsManager accessKey] withSecretKey:[ESCredentialsManager secretKey]];
        client.endpoint = [NSString stringWithFormat:@"https://%@", [ESRegionManager endpointForRegion:[ESRegionManager activeRegionObject]]];
        EC2DescribeInstancesResponse *response = [client describeInstances:[[EC2DescribeInstancesRequest alloc] init]];
        NSMutableArray *temp = @[].mutableCopy;
        for (EC2Reservation *reserv in response.reservations) {
            for (EC2Instance *instance in reserv.instances) {
                [temp addObject:instance];
            }
        }
        self.instances = temp;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    }
    @catch (NSError *error) {
        NSLog(@"Error: %@", error);
    }
});

但它仍然失败,出现同样的错误,应用程序崩溃。为什么我不能用这种方式捕捉这个错误?我该怎么办?

不要捕捉
n错误。Catch
NSException

所以我应该做
@Catch(NSError*error)
而不是
@Catch(NSException*exception)
?为什么会有不同?它成功了!但我不知道为什么——这两者有什么不同?这不是Java<代码>n错误
与异常无关。它只是一个用来保存错误细节的类。实际异常是基于
NSException
class.am执行相同的操作,但它不起作用-@catch(AmazonServiceException*serviceException){//Handle serviceException.}@catch(AmazonClientException*clientException){//Handle clientException.}但由于未捕获的异常“AmazonClientException”,仍导致其ma应用程序崩溃并终止应用程序,原因是:“指定的文件无法重新生成?”?