【发布时间】:2012-10-20 03:18:27
【问题描述】:
我正在从服务器加载数据,但有时服务器未连接(错误)。
我想用 Try/Catch 之类的东西来避免错误应用
1:在加载数据时尝试/cactch 2:尝试/捕捉图像
不知道怎么用 我写的代码是:
@try
{
dispatch_async(htvque, ^{
NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString:listChannel]];
NSError* error;
jsonTable = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (error) {
NSLog(@"%@", error);
}
else
{
NSMutableArray *arrImage = [jsonTable objectForKey:@"ListImage"];
for (int i =0; i<arrImage.count; i++) {
UIImage * result;
UIImageView *imgView = [[UIImageView alloc] init];
imgView.frame = CGRectMake(0, 30 * i , 20, 20);
imgView.image = result;
@try
{
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString: [arrImage objectAtIndex:i]]];
result = [UIImage imageWithData:data];
[self.view addSubview:imgView];
}
@catch
{}
@finally
{}
}
}
});
}
@catch(NSException * exp)
{
NSLOG(@"abc");
}
@finnaly
{
}
【问题讨论】:
-
您不应该在后台线程上编辑界面。另外,贴出exp的内容。
-
服务器死机时的错误,try/catch不使用,应用程序错误
标签: objective-c ios exception-handling error-handling try-catch