Ios catch块中未处理异常
这是我的代码,当我在UITextFields中输入错误的值时引发异常,但在引发异常后,它不会在catch语句中处理Ios catch块中未处理异常,ios,objective-c,ipad,Ios,Objective C,Ipad,这是我的代码,当我在UITextFields中输入错误的值时引发异常,但在引发异常后,它不会在catch语句中处理 - (IBAction)gotocanvas:(id)sender { @try { DrawObs_Probs *mainview = [self.storyboard instantiateViewControllerWithIdentifier:@"drawCanvas"]; mainview.length = self.length
- (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 {
}
}
-(iAction)gotocanvas:(id)发送方{
@试一试{
DrawObs_Probs*mainview=[self.storyboard实例化控件标识符:@“drawCanvas”];
mainview.length=self.length.text;
mainview.width=self.width.text;
mainview.camheight=self.camheight.text;
mainview.dataPath=self.dataPath;
如果([length.text floatValue]==0.0 | |[Width.text floatValue]==0.0 | |[camheight.text floatValue],如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];
}
-(iAction)gotocanvas:(id)发送方{
如果([length.text floatValue]==0.0 | |[Width.text floatValue]==0.0 | |[camheight.text floatValue]try..catch在Objective C中不是一种非常常见的编程模式。在这里,您可以简单地使用if语句来显示警报。您知道怎么做吗?只需将UIAlertView
代码放在您有NSException..
的地方,然后将[self-presentViewController:main view动画:是完成:否]
在else中,我和ClaushAnks做了相同的逻辑: