2

I have the following code in my program.

@try {

    float result =  4 / 0; // LINE 1

} @catch (NSException *e) {

    NSLog(@"Exception : %@", e);
    return 0;
}

I expected an exception to be caught in LINE 1 and thrown to the @catch block. But the execution aborts at LINE 1 showing EXC_ARITHMETIC in console.

What am I doing wrong here? What necessary things I have to do to do exception handling?

EmptyStack
  • 51,274
  • 23
  • 147
  • 178

6 Answers6

4

EXC_ARITHMETIC is a type of low-level exception known as a "signal". The only way to catch them is to register a signal handler, for example:

#include<signal.h>
void handler(int signal) {
    if (signal == FPE_FLTDIV)
        printf("Divide by 0 exception\n");
}

signal(SIGFPE, handler);    

However, the only safe thing to do in such a handler is clean up any resources and exit cleanly.

一二三
  • 21,059
  • 11
  • 65
  • 74
  • 3
    Actually, the only safe thing to do in such a handler is pretty much nothing (print a message and crash). Signal handlers will be executed on a random thread in an undefined state such that you pretty much can't do anything without risk of indeterminate behavior. – bbum Feb 10 '11 at 18:31
  • Mike Ash wrote an extensive article on signal handling and its pitfalls. http://mikeash.com/pyblog//friday-qa-2011-04-01-signal-handling.html – Jasarien Oct 06 '11 at 06:40
1

Divide by zero is not an NSException.

Give this a try (I never tried though):

@try {

    float result =  4 / 0; // LINE 1

} @catch (NSException *e) {

    NSLog(@"Exception : %@", e);
    return 0;
}
@catch (id ue) {

    //DIVIDE BY ZERO ATTEMPT MAY ENDUP HERE
    NSLog(@"Exception : %@", ue);
    return 0;
}

====== EDIT =======

Turns out divide by zero is not a obj-c exception. But seems you can catch such exceptions globally.

How do I catch global exceptions?

Community
  • 1
  • 1
Sarwar Erfan
  • 18,034
  • 5
  • 46
  • 57
0

Actually float result = 4 / 0; would never raise any kind of signal or exception (xcode, LLVM). No idea why it just silently returns inf into result.

Tertium
  • 6,049
  • 3
  • 30
  • 51
0

"4 / 0" is an expression with known literals, which can be very easily computed by compile time -- therefore there will be no runtime exception on that...

trudnai
  • 146
  • 1
  • 4
  • So you're saying that this is caught at *compile time* rather than *run time*, and so execution is aborted at the start of the file? – Andy Hayden Feb 14 '14 at 07:50
0

I am using the LLVM 5.1 compiler. In a *.m file just tried the line

float result = 4 / 0;

and

int result = 4 / 0;

and both give the result to be zero. But, because both contain the expression 4 / 0 which is integer division by zero regardless of the variable definition type, the compiler only gives a warning. All other compilers that I have used would have given an error from this expression.

The C language only specifies that integer division by zero is undefined. The compilers used in this case apparently define the result to be zero. This is a good example of why you should never depend on undefined behavior behaving the way you expect.

Simon G
  • 36
  • 2
0

Exceptions List and division by zero isn't a predefined exception. Also, to know the type of exception, you should send name message to the exception object.

NSLog(@"Exception : %@", [ e name ] );
Mahesh
  • 34,573
  • 20
  • 89
  • 115