gpt4 book ai didi

ios - 未在 catch block 中处理的异常

转载 作者:行者123 更新时间:2023-11-29 02:56:20 25 4
gpt4 key购买 nike

这是我的代码,当我在 UITextFields 中输入错误的值时会引发异常,但在引发异常后,catch 语句中不会对其进行处理。

- (IBAction)gotocanvas:(id)sender {
@try {

DrawObs_Probs *mainview = [self.storyboard instantiateViewControllerWithIdentifier:@"drawCanvas"];
mainview.length = self.length.text;
mainview.breadth = self.breadth.text;
mainview.camheight = self.camheight.text;
mainview.dataPath = self.dataPath;
if ([length.text floatValue]==0.0 || [breadth.text floatValue] == 0.0 || [camheight.text floatValue] <= 1.6) {
NSException* myException = [NSException exceptionWithName:@"Invalid" reason:@"Invalid Input Values" userInfo:nil];
@throw myException;
}
[self presentViewController:mainview animated:YES completion:nil];
}
@catch (NSException *exception) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Enter values in numbers" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alert show];
}
@finally {

}

}

最佳答案

正如 Paul 所说,不要使用 try..catch,而是使用下面的方法。

- (IBAction)gotocanvas:(id)sender {
if ([length.text floatValue] == 0.0 || [breadth.text floatValue] == 0.0 || [camheight.text floatValue] <= 1.6) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Enter values in numbers" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alert show];
return;
}

DrawObs_Probs *mainview = [self.storyboard instantiateViewControllerWithIdentifier:@"drawCanvas"];
mainview.length = self.length.text;
mainview.breadth = self.breadth.text;
mainview.camheight = self.camheight.text;
mainview.dataPath = self.dataPath;

[self presentViewController:mainview animated:YES completion:nil];
}

关于ios - 未在 catch block 中处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23863399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com