catch块中没有处理异常

时间:2014-05-26 05:41:16

标签: ios objective-c ipad

这是我的代码,当我在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 {

    }

}

1 个答案:

答案 0 :(得分:1)

正如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];
}