在ios开发中使用 try 和 catch 来捕获错误。

本文转载至 http://blog.csdn.net/remote_roamer/article/details/7105776

    1. 抛出错误的代码
      1. //如果返回的报文是错误信息,则抛出错误  
      2.    if([outParams count] <= 0)  
      3.    {  
      4.        [NSException raise:@"WebService error" format:@"%@", returnJson4SOAP];  
      5.    }  
    2. 在调用中捕获错误代码
      1. //从soap 信息中解析出CusotmerDetail 对象  
      2.     @try  
      3.     {  
      4.         customerDetail = [[[SoapRtnJsonParser alloc] init] parse2CustomerDtail:[returnSoapXML dataUsingEncoding:NSUTF8StringEncoding]];  
      5.     }@catch (NSException * e) {  
      6.         NSLog(@"Exception: %@", e);  
      7.         UIAlertView * alert =  
      8.         [[UIAlertView alloc]  
      9.          initWithTitle:@"错误"  
      10.          message: [[NSString alloc] initWithFormat:@"%@",e]  
      11.          delegate:self  
      12.          cancelButtonTitle:nil  
      13.          otherButtonTitles:@"OK", nil];   
      14.         [alert show];  
      15.         [alert release];  
      16.         return;  
      17.     }  
posted @ 2014-07-05 15:18  天牛  阅读(236)  评论(0编辑  收藏  举报