search

Home  >  Q&A  >  body text

objective-c - NSException与NSError区别?

NSException与NSError在使用中有哪些区别?

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    
    @autoreleasepool {
        NSException* ex = [[NSException alloc]initWithName:@"MyException"  
                                                    reason:@"b==0"   
                                                  userInfo:nil];  
        
        @try   
        {  
            int b = 0;   
            switch (b)    
            {  
                case 0:  
                    @throw(ex);//b=0,则抛出异常;  
                    break;  
                default:  
                    break;  
            }  
        }  
        @catch (NSException *exception)//捕获抛出的异常   
        {  
            NSLog(@"exception.name= %@" ,exception.name);
            NSLog(@"exception.reason= %@" ,exception.reason);
            NSLog(@"b==0 Exception!");  
        }  
        @finally   
        {  
            NSLog(@"finally!");  
        }  
        [ex release];  
        
    }
    return 0;
}

https://developer.apple.com/library/m...

https://developer.apple.com/library/m...

怪我咯怪我咯2765 days ago452

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-04-21 11:17:57

    Exceptions and errors are two completely different things. If an exception is thrown but you don't catch it, it will crash. But if an error is returned, you can do whatever you want to the outside world. It just tells you that there is an error in this call.

    reply
    0
  • Cancelreply