【问题标题】:iOS exception handling problemiOS异常处理问题
【发布时间】:2011-11-02 13:41:21
【问题描述】:

我在下面的代码中有问题。

int i = 10;
NSString *str;
@try {
    //Error
    str = [[NSString alloc] initWithFormat:@"%@",i];
    //Warning
    NSLog(@"%i",str);
}
@catch (id exception) {
    NSLog(@"Hello !!!");
}

请帮助我了解如何处理 i/o 操作中的异常。
谢谢。

贾斯汀编辑

我没有合适的地方给你提供这个程序,所以我把它放在这里。

尝试使用以下程序重新表述您的问题:

static void bad_program() {

    @try {
        id array = [NSArray array];
        NSLog(@"will throw...");
        [array stringByAbbreviatingWithTildeInPath];
    }
    @catch (id exception) {
        NSLog(@"\n*** Caught Exception:\n***** Exception Detail: %@\nResolution: Exception swallowed\n", exception);
    }
}

@贾斯汀,上述代码的结果
它不会触发异常。它崩溃并显示以下错误。
对不起贾斯汀,请看看你能不能帮助我更多。谢谢。

2011-08-25 15:06:48.444 TestError[5120:b303] will throw...
2011-08-25 15:06:48.446 TestError[5120:b303] -[__NSArrayI stringByAbbreviatingWithTildeInPath]: unrecognized selector sent to instance 0x4b38e10
2011-08-25 15:06:48.465 TestError[5120:b303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI stringByAbbreviatingWithTildeInPath]: unrecognized selector  sent to instance 0x4b38e10'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00dc05a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x00f14313 objc_exception_throw + 44
    2   CoreFoundation                      0x00dc20bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x00d31966 ___forwarding___ + 966
    4   CoreFoundation                      0x00d31522 _CF_forwarding_prep_0 + 50
    5   TestError                           0x00002454 -[TestErrorViewController viewDidLoad] + 164
    6   UIKit                               0x000c2089 -[UIViewController view] + 179
    7   UIKit                               0x00035d42 -[UIWindow addRootViewControllerViewIfPossible] + 51
    8   TestError                           0x00002087 -[TestErrorAppDelegate application:didFinishLaunchingWithOptions:] + 135
    9   UIKit                               0x00012c89 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163
    10  UIKit                               0x00014d88 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 439
    11  UIKit                               0x0001f617 -[UIApplication handleEvent:withNewEvent:] + 1533
    12  UIKit                               0x00017abf -[UIApplication sendEvent:] + 71
    13  UIKit                               0x0001cf2e _UIApplicationHandleEvent + 7576
    14  GraphicsServices                    0x00ff9992 PurpleEventCallback + 1550
    15  CoreFoundation                      0x00da1944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    16  CoreFoundation                      0x00d01cf7 __CFRunLoopDoSource1 + 215
    17  CoreFoundation                      0x00cfef83 __CFRunLoopRun + 979
    18  CoreFoundation                      0x00cfe840 CFRunLoopRunSpecific + 208
    19  CoreFoundation                      0x00cfe761 CFRunLoopRunInMode + 97
    20  UIKit                               0x000147d2 -[UIApplication _run] + 623
    21  UIKit                               0x00020c93 UIApplicationMain + 1160
    22  TestError                           0x00001fc9 main + 121
    23  TestError                           0x00001f45 start + 53
    24  ???                                 0x00000001 0x0 + 1
)
terminate called throwing an exceptionsharedlibrary apply-load-rules all

【问题讨论】:

  • 打开你的编译器警告并注意它们:)
  • @Justin 我想对该语句触发异常,但它不会触发异常但它崩溃了。
  • 它崩溃了,因为您将一个 int 作为 NSObject 传递,然后通过 varargs 将一个 NSString 作为 int 传递。编译器会警告您这些问题(因此是我的原始评论)。我更新了问题,为您提供了一个投掷和接球的程序,因此您可以更轻松地提出问题。
  • @Justin,上面的代码也崩溃了。
  • 与之前的 cmets 一样,您的字符串格式不正确,您应该使用 %d 而不是 %@。 NSLog 字符串也很糟糕;使用 %@ (或 %d 如果你想要指针地址)而不是 %i。

标签: iphone objective-c ios cocoa-touch exception-handling


【解决方案1】:

我得到了这个问题的答案。
有些异常无法捕获,有些异常仅适用于不在模拟器上的设备。

【讨论】:

  • 请注意,您永远不应该在 iOS 或 OSX 上捕获并尝试从异常中恢复。在这些环境中无法恢复异常。
猜你喜欢
  • 2016-06-27
  • 1970-01-01
  • 2011-01-18
  • 2011-04-27
  • 1970-01-01
相关资源
最近更新 更多