在ios开发中使用 try 和 catch 来捕获错误。
来源:互联网 发布:冒险岛夜光法师v矩阵 编辑:程序博客网 时间:2024/04/30 03:26
ios中很少用到try 和catch
简单的来说,Apple虽然同时提供了错误处理(NSError)和异常处理(exception)两种机制,但是Apple更加提倡开发者使用NSError来处理程序运行中可恢复的错误。而异常被推荐用来处理不可恢复的错误。
原因有几个,在非gc情况下,exception容易造成内存管理问题(文档有描述即使是arc下,也不是安全的);exception使用block造成额外的开销,效率较低等等,另外这也的确是Cocoa开发者的习惯。
1,抛出错误的代码
1
2
3
4
5
//如果返回的报文是错误信息,则抛出错误
if
([outParams count] <=
0
)
{
[NSException raise:@
"WebService error"
format:@
"%@"
, returnJson4SOAP];
}
2,在调用中捕获错误代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//从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
;
}
0 0
- 在ios开发中使用 try 和 catch 来捕获错误。
- 在ios开发中使用 try 和 catch 来捕获错误。
- 在ios开发中使用 try 和 catch 来捕获错误。
- iOS中try-catch-finally捕获异常使用
- iOS中@try@catch@finally捕获异常使用
- iOS中@try@catch@finally捕获异常使用
- iOS中@try@catch@finally捕获异常使用
- 在swift中使用try catch 捕获异常
- 使用try和catch捕获异常
- 使用try catch抛出和捕获异常
- ios开发try catch使用
- iOS中 @try 和@catch
- php try catch捕获错误
- 通过C++的try和catch来捕获SEH异常
- 使用try/catch捕获异常
- SQL2005存储过程中使用try catch捕获异常
- iOS开发- try catch
- Java中使用try...catch以及try...catch...finally捕获异常
- Android中程序与Service交互的方式
- mysql5.5 开启慢查询日志
- 第七周项目求两个数的正差值
- ExtJs的一些常用控件属性
- 杂谈随想第001篇:对于“不死鸟”反驳姚仙言论的看法
- 在ios开发中使用 try 和 catch 来捕获错误。
- Hadoop中SequenceFile的使用
- 【转】提高Android应用程序的速度四大原则
- Android中shape的使用
- ld: 1 duplicate symbol for architecture armv7 Xcode 报错了 :
- 【android内核分析-输入输出】Android4.0 input touch解析
- 别样JAVA学习(七)多线程上
- UML,理理关系
- 第七周 项目二 并联电阻