OC 异常处理

关键字:

@try @catch @finally @throw

注意:当前catch到的异常可以重新throw出去,但当前的异常处理会先执行完毕再执行其他的。

int main(int argc, char* argv[]) {
    @try{
        @try {
            NSException *e = [NSException exceptionWithName: @"myException" reason: @"fuck reason" userInfo: [[NSDictionary alloc] init]];
            @throw  e;
            printf("try\n");
        }
        @catch (NSException *e){
            printf("inner catch\n");
            @throw e;
        }
        @finally{
            printf("finally\n");
        }
    }
    @catch (NSException* e){
        printf("outer catch\n");
    }
    return 0;
}

上面代码中catch到的异常throw了出去,但还是会先执行finally,再执行outer catch:

 

posted @ 2020-12-02 02:02  NeoZy  阅读(178)  评论(0编辑  收藏  举报