【问题标题】:Why can't I catch this iOS AWS SDK network error?为什么我无法捕获此 iOS AWS 开发工具包网络错误?
【发布时间】:2014-02-05 01:55:52
【问题描述】:

我在我的应用程序中使用AWS iOS SDK,我最近测试了无网络情况。这是我的代码:

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);
    }
});

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

【问题讨论】:

    标签: ios objective-c amazon-web-services try-catch


    【解决方案1】:

    不要抓住NSError。抓住NSException

    【讨论】:

    • 所以我应该做 @catch (NSError *error) 而不是 @catch (NSException *exception) ?为什么会有所不同?
    • 成功了!不过,我不确定为什么 - 两者有什么不同?
    • 这不是 Java。 NSError 与异常无关。它只是一个用于保存有关错误的详细信息的类。实际异常基于NSException 类。
    • 我在做同样的事情,但它不起作用 -@catch (AmazonServiceException *serviceException) { // 处理 serviceException。 } @catch (AmazonClientException *clientException) { // 处理客户端异常。但由于未捕获的异常“AmazonClientException”,它的 ma 应用程序仍然会崩溃,原因是:“指定的文件无法重新考虑?
    猜你喜欢
    • 2013-11-22
    • 1970-01-01
    • 2021-03-21
    • 2015-04-15
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 2021-11-04
    相关资源
    最近更新 更多