iOS exception handling problem

2

I have problem in following code.

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

Please help me out how to handle exception in i/o operation.
Thanks.

Edited by Justin

there's no suitable place for me to provide this program to you, so i put it here.

try using the following program to reformulate your question:

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, Result of above code
It does not fire exception. It is crashed showing following error.
Sorry Justin, please see if you could help me more. Thanks.

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
iphone
objective-c
ios
cocoa-touch
exception-handling
asked on Stack Overflow Aug 24, 2011 by iOS Dev • edited Nov 9, 2011 by User97693321

1 Answer

0

I got answer of this question.
Some exception cannnot catch and some exception will only work for device not on the simulator.

answered on Stack Overflow Sep 6, 2011 by iOS Dev

User contributions licensed under CC BY-SA 3.0