How to enable Exception Handling on XCode 3.2.3?
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.
Dividing by zero does not raise a C++ exception. See this question.
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".
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.
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.
精彩评论