可以用在代码的调试上:

-(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:

 

iOS 利用异常 NSException 调试代码

关于异常的处理:

 

        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");  

  }

相关文章:

  • 2021-12-08
  • 2021-10-11
  • 2022-01-12
  • 2021-08-07
  • 2021-11-08
  • 2021-10-22
  • 2021-11-15
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-19
  • 2021-08-18
  • 2022-12-23
  • 2021-08-17
  • 2021-05-21
相关资源
相似解决方案