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

  1. 抛出错误的代码
     //如果返回的报文是错误信息,则抛出错误
        if([outParams count] <= 0)
        {
            [NSException raise:@"WebService error" format:@"%@", returnJson4SOAP];
        }
  2. 在调用中捕获错误代码
    //从soap 信息中解析出CusotmerDetail 对象
        @try
        {
            customerDetail = [[[SoapRtnJsonParser alloc] init] parse2CustomerDtail:[returnSoapXML dataUsingEncoding:NSUTF8StringEncoding]];
        }@catch (NSException * e) {
            NSLog(@"Exception: %@", e);
            UIAlertView * alert =
            [[UIAlertView alloc]
             initWithTitle:@"错误"
             message: [[NSString alloc] initWithFormat:@"%@",e]
             delegate:self
             cancelButtonTitle:nil
             otherButtonTitles:@"OK", nil]; 
            [alert show];
            [alert release];
            return;
        }



你可能感兴趣的:(xcode(Iphone)编程)