1

How to enable exceptions in XCode - 3.2.3. Is there any flag like I should enable for the compiler for exception handling? Tried googling but didn't find enough information on XCode with C++ !

#include <iostream>
#include <exception>

int main()
{
    try
    {
        int i=5,j=0;
        int res = i/j;
    }
    catch (const std::exception& exe) 
    {
        std::cerr<< exe.what();
    }
    catch (...)
    {
        std::cout<< "\n Default Exception Handler \n";
    }

    return 0;
}

Output:

Loading program into debugger…
Program loaded.
run
[Switching to process 1332]
Running…
Program received signal: “EXC_ARITHMETIC”.
sharedlibrary apply-load-rules all
kill
Current language: auto; currently c++
quit
The Debugger has exited with status 0.(gdb)

Edit :Though the reason seems to be different, to anyone, this figure might be helpful in future.

Mahesh
  • 34,573
  • 20
  • 89
  • 115

4 Answers4

1

I'm pretty sure exception handling is on by default, but I don't think division by zero actually generates an exception. If you want to make sure they are on though, just go to your project or target settings, and search for "exception"; there's a checkbox called "Enable C++ Exceptions".

crimson_penguin
  • 2,728
  • 1
  • 17
  • 24
1

A CPU exception such as an arithmetic exception like divide by zero above is not a C++ exception. People who have only ever used Microsoft Visual C++ often get confused by this, since Microsoft added a non-standard extension which allows CPU exceptions to be treated as C++ exceptions, but this is not the norm and is of course not portable.

Paul R
  • 208,748
  • 37
  • 389
  • 560
1

Dividing by zero does not raise a C++ exception. See this question.

Community
  • 1
  • 1
Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
0

The way you are trying to handle the exception is proper ... that will work in the exception flows.

Not: The EXC_ARITHMETIC (devision by 0) is not an exception it is a signal -- so you have to use signal handlers to handle this.

Girish Kolari
  • 2,515
  • 2
  • 24
  • 34