iosobjective-cdivide-by-zerodividebyzeroexception

Divide by zero exception - Objective-C


So I'm taking an Xcode Objective-C course and we have to make a calculator. Everything works except for the part where I have to create an exception for if someone tries to divide by zero. I cannot for the life of me get it to work and all I keep getting is the INF message on my calculator. Also they want it to be a @try @catch method for it so my Google searches have lead me to ask the question myself since I can't find the answer. Here is my complete code.

#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

Solution

  • Instead of setting:

    result = NAN;
    

    when you divide by zero you should

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

    Don't catch the exception in the divideOp method... catch it in whatever routine is supposed to print the result of the calculation and print out that an illegal divide by zero operation was attempted instead.