2013年6月18日 星期二

objective C 也有 try catch方法

 NSString *test = @"test";
    unichar a;
    int index = 5;

    @try {
        a = [test characterAtIndex:index];
    }
    @catch (NSException *exception) {
        NSLog(@"%@", exception.reason);
    }
    @finally {
        NSLog(@"Char at index %d cannot be found", index);
        NSLog(@"Max index is: %d", [test length]-1);
    }
Log:
[__NSCFConstantString characterAtIndex:]: Range or index out of bounds
Char at index 5 cannot be found
Max index is: 3

沒有留言:

張貼留言