iOS异常处理问题

时间:2011-08-24 07:31:23

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

我在下面的代码中遇到问题。

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

@ Justin,上述代码的结果
它不会引发异常。崩溃显示以下错误 对不起贾斯汀,请看看你是否可以帮助我。感谢。

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

1 个答案:

答案 0 :(得分:0)

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