2011-08-24 137 views
2

我在下面的代碼中遇到問題。iOS異常處理問題

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

請幫我解決在I/O操作中如何處理異常。
謝謝。

由Justin編輯

還有爲我提供這個程序對你沒有合適的地方,所以我把它放在這裏。

嘗試使用下面的程序重新制定你的問題:

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 
+0

打開你的編譯器警告,並注意它們:) – justin

+0

@賈斯汀我想在該語句上觸發異常,但它不會觸發異常,但它崩潰。 –

+1

它崩潰,因爲你傳遞一個int作爲一個NSObject,後來一個NSString作爲一個int通過可變參數。編譯器會警告你這些問題(因此我原來的評論)。我更新了這個問題,爲您提供了一個拋出和捕獲的程序,以便您更輕鬆地提出問題。 – justin

回答

0

我得到了這個問題的答案。
某些異常無法捕捉,某些異常僅適用於不在模擬器上的設備。

+0

請注意,您絕對不應該在iOS或OSX上捕捉並嘗試從異常中恢復。在這些環境中,異常無法恢復。 – bbum