I created a C++ library. I am using it in an iOS application. I thought of handling exceptions in C++ library and in order to test this I created a test scheme and called the c++ function from it. In the c++ function I intentionally wrote erroneous code.
try
{
int i = 0;
int j = 10/i;
}
catch(std::exception &e){
printf("Exception : %s\n",e.what());
}
But the exception is never caught and the application breaks at – int j = 10/i;
Please tell me, how can I add exception handling to my c++ code in this scenario?
Thanks.
C++ doesn’t throw an exception when a division by zero is encountered (see also this and this other questions)! Quoting Stroustrup:
“The Design and Evolution of C++” (Addison Wesley, 1994)
So you will have to handle this case yourself – you’ll have to check each potential divisor before division and handle it.