首页 > 移动平台 > 详细

iOS 利用异常 NSException 调试代码

时间:2015-12-12 18:40:26      阅读:432      评论:0      收藏:0      [点我收藏+]

可以用在代码的调试上:

-(instancetype)init{

    

    @throw [NSException exceptionWithName:@"Singleton" reason:@"Use [GloabData shareData]" userInfo:nil];

    

    return  nil;

}

或者:

 NSException * e = [[NSException alloc] initWithName:@"Singleton" reason:@"Use [GloabData shareData]" userInfo:nil];

 

    @throw e;

 

 

抛出了一个bug:

 

技术分享

关于异常的处理:

 

        NSException* ex = [[NSException alloc] initWithName:@"ExceptionName"   // just for test                                        
     reason:@"XXX"
         userInfo:nil];
     CustomNSException* ex = [[CustomNSException alloc] initWithName:@"CustomNSExceptionName" // just for test reason:@"XXX" userInfo:nil];

@try { bool error = YES;  

         if (error) {  

         @throw ex;  

               }  

}

    @catch ( CustomNSException *exception ) {     
        NSLog(@"CustomNSException.name = %@" ,CustomNSException.name);
       NSLog(@"CustomNSException.reason = %@" , CustomNSException.reason);

UIAlertView* alert = [[UIAlertView alloc]   initWithTitle:CustomNSException.name  message:CustomNSException.reason                                         delegate:nil  cancelButtonTitle:nil   otherButtonTitles:nil];  

      [alert show]; 

}  

@catch ( NSException *exception ) {  

     NSLog(@"exception.name = %@" , exception.name);      

    NSLog(@"exception.reason = %@" , exception.reason);  

}  

@finally {  

        NSLog(@"@finally");  

  }

iOS 利用异常 NSException 调试代码

原文:http://www.cnblogs.com/feiyafeiblog/p/5041537.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!