美文网首页
Objective-C - 异常处理(NSException)

Objective-C - 异常处理(NSException)

作者: 木马不在转 | 来源:发表于2017-07-21 14:42 被阅读188次

    苹果关于异常的详细文档
    关于自定义异常或者扩展:
    Objective-C中处理异常是依赖于NSException实现的,它是异常处理的基类,它是一个实体类,而并非一个抽象类,所以你可以直接使用它或者继承它扩展使用:
    1.直接使用,分两种,抛出默认的异常,和自定义自己的新的种类的异常:
    OC代码

    #import 
    intmain (intargc,constchar* argv[])
    {
    @autoreleasepool {
    NSException* ex = [[NSException alloc]initWithName:@"MyException"
    reason:@"b==0"
    userInfo:nil];
    @try
    {
    intb = 0;
    switch(b)
    {
    case0:
    @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];
    }
    return0;
    }
    

    ps:
    Initializes and returns a newly allocated exception object.

    - (id)initWithName:([NSString](http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/c_ref/NSString)*)*name*reason:([NSString](http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/c_ref/NSString)*)*reason*userInfo:([NSDictionary](http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSDictionary)*)*userInfo*
    Parameters
    *name*
    The name of the exception.
    *reason*
    A human-readable message string summarizing the reason for the exception.
    *userInfo*
    A dictionary containing user-defined information relating to the exception
    Return Value
    The createdNSExceptionobject ornilif the object couldn't be created.
    Discussion
    This is the designated initializer.
    Availability
    Available in iOS 2.0 and later.
    

    2.扩展使用,这个推荐你需要自定义一些功能的时候使用,比如,当捕获到指定的异常的时候弹出警告框之类的:
    OC代码

    ![](https://img.haomeiwen.com/i1823354/533e19d2a925a0c8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    @interface MyException : NSException
    -(void)popAlert
    @end
    Java代码
    ![](https://img.haomeiwen.com/i1823354/4160a2c42246f2e6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    @implementationMyException
    - (void)popAlert
    {
    //弹出报告异常原因的警告框 reason
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Tips"
    message:self.reason
    delegate:nil
    cancelButtonTitle:nil
    otherButtonTitles:nil];
    [alert show];
    [alert release];
    }
    @end
    

    使用:
    OC代码

    - (IBAction)btnClicked_Exception:(id)sender
    {
    MyException* ex = [[MyException alloc]initWithName:@"MyException"
    reason:@"除数为0了!"
    userInfo:nil];
    @try
    {
    intb = 0;
    switch(b)
    {
    case0:
    @throw(ex);//b=0,则抛出异常;
    break;
    default:
    break;
    }
    }
    @catch(MyException *exception)//捕获抛出的异常
    {
    [exception popAlert];
    NSLog(@"b==0 Exception!");
    }
    @finally
    {
    NSLog(@"finally!");
    }
    [ex release];
    }
    

    这个时候,捕获到异常,它就会弹出警告框了。当然,你还可以在MyException里面加一些指定的异常的通用处理方法。
    只要你愿意,你就可以随意的定制它!

    相关文章

      网友评论

          本文标题:Objective-C - 异常处理(NSException)

          本文链接:https://www.haomeiwen.com/subject/szkekxtx.html