VS C++ throwing divide by zero exception after a specific check
- by Dr. Monkey
In the following C++ code, it should be impossible for ain integer division by zero to occur:
// gradedUnits and totalGrades are both of type int
if (gradedUnits == 0) {
return 0;
} else {
return totalGrades/gradedUnits; //call stack points to this line
}
however Visual Studio is popping up this error:
Unhandled exception at 0x001712c0 in DSA_asgn1.exe: 0xC0000094: Integer division by zero.
And the stack trace points to the line indicated in the code.
It seems like VS might just do this with any integer division, without checking whether a divide by zero is possible. Do I need to catch this exception even though the code should never be able to throw it? If so, what's the best way to go about this?
This is for an assignment that specifies VS 2005/2008 with C++. I would prefer not to make things more complicated than I need to, but at the same time I like to do things properly where possible.