除以零异常 - Objective-C

Divide by zero exception - Objective-C

所以我要上 Xcode Objective-C 课程,我们必须制作一个计算器。一切正常,除了我必须为有人试图除以零而创建异常的部分。我终其一生都无法使它正常工作,而我一直收到的只是计算器上的 INF 消息。他们还希望它成为一个 @try @catch 方法,所以我的 Google 搜索导致我自己问这个问题,因为我找不到答案。这是我的完整代码。

#import "Calculator.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize numerator, denominator, total, operand, theNumber, lblText;

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setNumerator:0];
    [self setDenominator:0];
    [self setOperand:PLUS];
    [self setTotal:0.0];
    [self setTheNumber:@""];

    [self printNumber];
}

-(void)printNumber
{
    [lblText setText:theNumber];
}

-(float)divideOp
   {
    float result=0;
    @try {
        if (self.denominator != 0) {
            result = (self.numerator / self.denominator);
        } else {
            result = NAN;
        }
    } @catch (NSException *exception) {
        // deal with the exception
        NSLog(@"Can't divide by 0!");
    }

    return result;
}

-(IBAction)calculate:(id)sender
{
    denominator = [theNumber integerValue];

    if(operand == PLUS)
        total = numerator + denominator;
    else if(operand == MINUS)
        total = numerator - denominator;
    else if(operand == MULTIPLY)
        total = numerator * denominator;

    theNumber = [NSString stringWithFormat:@"%f",total];
    [self printNumber];

    numerator = 0;
    denominator = 0;
    total = 0.0;
}

-(void)saveNumerator
{
    numerator = [theNumber integerValue];
    theNumber = @"0";
    [self printNumber];
}

-(IBAction)clearNum:(id)sender
{
    theNumber = @"0";
    [self printNumber];

}
-(IBAction)setPlus:(id)sender
{
    [self saveNumerator];
    operand = PLUS;
}

-(IBAction)setMinus:(id)sender
{
    [self saveNumerator];
    operand = MINUS;
}

-(IBAction)setMultiply:(id)sender
{
    [self saveNumerator];
    operand = MULTIPLY;
}

-(IBAction)setDivide:(id)sender
{
    [self saveNumerator];
    operand = DIVIDE;
}

-(IBAction)press9:(id)sender
{
    theNumber = [theNumber stringByAppendingString:@"9"];
    [self printNumber];
}

-(IBAction)press8:(id)sender
{
    theNumber = [theNumber stringByAppendingString:@"8"];
    [self printNumber];
}

-(IBAction)press7:(id)sender
{
    theNumber = [theNumber stringByAppendingString:@"7"];
    [self printNumber];
}

-(IBAction)press6:(id)sender
{
    theNumber = [theNumber stringByAppendingString:@"6"];
    [self printNumber];
}

-(IBAction)press5:(id)sender
{
    theNumber = [theNumber stringByAppendingString:@"5"];
    [self printNumber];
}

-(IBAction)press4:(id)sender
{
    theNumber = [theNumber stringByAppendingString:@"4"];
    [self printNumber];
}

-(IBAction)press3:(id)sender
{
    theNumber = [theNumber stringByAppendingString:@"3"];
    [self printNumber];
}

-(IBAction)press2:(id)sender
{
    theNumber = [theNumber stringByAppendingString:@"2"];
    [self printNumber];
}

-(IBAction)press1:(id)sender
{
    theNumber = [theNumber stringByAppendingString:@"1"];
    [self printNumber];
}

-(IBAction)press0:(id)sender
{
    theNumber = [theNumber stringByAppendingString:@"0"];
    [self printNumber];
}

@end

而不是设置:

result = NAN;

当你除以零时你应该

NSException *e = [NSException
    exceptionWithName:@"Divide By Zero"
    reason:@"Divide by Zero Exception"
    userInfo:nil];
@throw e;

不要在 divideOp 方法中捕获异常...在应该打印计算结果的任何例程中捕获它,并打印出试图进行非法除零操作。