0

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
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Brooke Taylor
  • 131
  • 2
  • 3
  • 11
  • Please only post relevant code. Most of the code you posted has nothing to do with your question. – rmaddy May 06 '17 at 05:58
  • And judging by your very outdated use of `@synthesize`, I urge you to find a much more up-to-date tutorial. – rmaddy May 06 '17 at 05:59

1 Answers1

0

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.

Scott Thompson
  • 22,629
  • 4
  • 32
  • 34
  • So I would need to put what you just put where the @catch currently is and move that entire thing into the IBAction section? Sorry this is literally my first week of this class as in started on Tuesday so it's hard to understand what you mean by not catching the exception in the divideOp method. – Brooke Taylor May 06 '17 at 03:31