NSException raise:format: as the last statement in a method

后端 未结 3 440
滥情空心
滥情空心 2021-01-19 17:52

I have this method:

+ (MHTwitterParser*)createParser:(NSString*)format {
    if ([format compare:@\"json\"] == NSOrderedSame) {
        return [[MHJsonTwitt         


        
3条回答
  •  走了就别回头了
    2021-01-19 18:44

    The reason is simple.

    For the compiler, the method [NSException raise: ...] is a black box method. It doesn't know that the method will actually raise an exception.

    If you compare it with Java or C++, their throw statements are a language feature and the compiler knows exactly what will happen when it finds it. In Obj-C it's different and sometimes it depends on runtime conditions. Consider the following.

    NSException* exception = nil;
    
    if (someCondition) {
    
       exception = [NSException exceptionWithName:...];
    }
    
    [exception raise];
    

    The compiler won't know if the exception is really raised or not.

提交回复
热议问题