catching a divide-by-zero error
-
I am trying to catch a divide-by-zero error with the following code:
#include
using namespace std;int main() {
try {
int a = 0;
int x = 5 / a;
}
catch (exception e)
{
cout << e.what() << endl;
}
catch (...)
{
cout << "caught ..." << endl;
}}
For some reason, neither of my catch blocks gets executed. Anyone know how to catch the exception? Thank you.
-
I am trying to catch a divide-by-zero error with the following code:
#include
using namespace std;int main() {
try {
int a = 0;
int x = 5 / a;
}
catch (exception e)
{
cout << e.what() << endl;
}
catch (...)
{
cout << "caught ..." << endl;
}}
For some reason, neither of my catch blocks gets executed. Anyone know how to catch the exception? Thank you.
AFAIK, there's no way to do it. You'll have to catch it manually and throw.
CI/CD = Continuous Impediment/Continuous Despair
-
I am trying to catch a divide-by-zero error with the following code:
#include
using namespace std;int main() {
try {
int a = 0;
int x = 5 / a;
}
catch (exception e)
{
cout << e.what() << endl;
}
catch (...)
{
cout << "caught ..." << endl;
}}
For some reason, neither of my catch blocks gets executed. Anyone know how to catch the exception? Thank you.
I've not used it, but maybe take a look at Handling the Divide by Zero Exception in C++[^], similar to what Maximilien said.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
I am trying to catch a divide-by-zero error with the following code:
#include
using namespace std;int main() {
try {
int a = 0;
int x = 5 / a;
}
catch (exception e)
{
cout << e.what() << endl;
}
catch (...)
{
cout << "caught ..." << endl;
}}
For some reason, neither of my catch blocks gets executed. Anyone know how to catch the exception? Thank you.
The article mentioned in the previous post does it by doing divisions in a function that checks for a zero divisor and throws an exception if it detects one. If you don't want to use such a function, it gets rather complicated. If you're running on Windows, you need to handle the structured exception
STATUS_FLOAT_DIVIDE_BY_ZERO
orSTATUS_INT_DIVIDE_BY_ZERO
. If you're running on Linux, you need to handle the POSIX signalSIGFPE
. You can also handle the use of a bad pointer with these techniques. The handler that receives the structured exception or POSIX signal can throw a C++ exception, which you can then catch in the usual way. The following article goes into the details. But because it also describes a thread framework, you'll have to sift through it to extract the specific code that you need. In particular, see the sections "Receiving a Windows Structured Exception" and "Receiving a POSIX Signal". Robust C++: Safety Net[^]Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
I am trying to catch a divide-by-zero error with the following code:
#include
using namespace std;int main() {
try {
int a = 0;
int x = 5 / a;
}
catch (exception e)
{
cout << e.what() << endl;
}
catch (...)
{
cout << "caught ..." << endl;
}}
For some reason, neither of my catch blocks gets executed. Anyone know how to catch the exception? Thank you.
-
I am trying to catch a divide-by-zero error with the following code:
#include
using namespace std;int main() {
try {
int a = 0;
int x = 5 / a;
}
catch (exception e)
{
cout << e.what() << endl;
}
catch (...)
{
cout << "caught ..." << endl;
}}
For some reason, neither of my catch blocks gets executed. Anyone know how to catch the exception? Thank you.
In Windows, there are additional, O/S-level exceptions, that provide this sort of information. Look for "Structured Exception Handling". If you use MSVC, you may use the _set_se_handler function in main() and in the main function of each thread to convert Structured Exceptions to C++ exceptions. There may be similar O/S- and compiler-specific methods in other environments.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.