使用@try处理异常

作者:追风剑情 发布于:2019-2-22 16:39 分类:Objective-C

示例

#import <Foundation/Foundation.h>
#import "Fraction.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //主动抛出异常
        //@throw [NSException exceptionWithName:@"context show" reason:@"crush's reason" userInfo:nil];
        
        Fraction *f = [[Fraction alloc] init];

        @try {
            [f noSuchMethod];
        }
        @catch (NSException *exception) {
            NSLog(@"Caught %@%@", [exception name], [exception reason]);
        }
        @finally {
            NSLog(@"finally...");
        }
        
        NSLog(@"Execution continues!");
    }
    return 0;
}

标签: Objective-C

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号