Reputation: 34605
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.
Upvotes: 1
Views: 2834
Reputation: 2515
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.
Upvotes: 0
Reputation: 84151
Dividing by zero does not raise a C++ exception. See this question.
Upvotes: 1
Reputation: 212929
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.
Upvotes: 1
Reputation: 2778
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".
Upvotes: 1