0

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.

UPDATE: I may have just been doing something silly here. While messing around trying to get VS to pay attention to my debug breakpoints, I rebuilt the solution and the exception isn't happening any more. It seems likely to me that I was stopping in the middle of a debug session and resuming it, when I thought I was starting new sessions.

Thanks for the responses. Would it be appropriate here to delete my question since it's resolved and wasn't really what I thought it was?

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.

Community
  • 1
  • 1
David Mason
  • 2,917
  • 4
  • 30
  • 45
  • A divide by zero _looks_ impossible here. But we could use more context. Could another thread be changing gradedUnits? Would you post the assembly bytes/instructions that the compiler turns this into? Perhaps the optimizer is doing something unexpected? – John Knoeller Mar 27 '10 at 07:48
  • This is a runtime exception. Try to run it in a debugger and see values – Eli Bendersky Mar 27 '10 at 07:54
  • You seem to be confused about how exceptions work. I'm betting you come from a Java background? That exception is thrown at runtime when you divide by zero, not at compile time just to let you know there's a possible exception that might occur. So while that exception *should* never occur, it actually is, and you need to investigate. – Dennis Zickefoose Mar 27 '10 at 08:10
  • I tried out this code just in case and it didn't happen to me. There must be something more going on here than is obvious just from that code. – jcoder Mar 27 '10 at 08:34
  • Regarding *[..] to delete my question [..]* : You could just post an answer to your own question, letting others know that if they get this error, first try a rebuild – default Mar 30 '10 at 07:56

2 Answers2

1

You should try going through this code with VS debugger, and see what are the actual values of those variables.

Sad Developer
  • 1,258
  • 1
  • 12
  • 16
-1

It turns out this problem was caused by the code I originally had, which did not have the divide-by-zero check, shown here:

return totalGrades/gradedUnits;

The problem was that although I'd updated the code, I was actually still in the same debug session that threw the original error, so the program was still running on the old code and threw the error again every time I restarted it.

The problem was solved by rebuilding the solution, which forced a new debug session. Simply terminating the debug session and restarting it with a rebuild would have solved it too (I just hadn't noticed I was still in the session).

David Mason
  • 2,917
  • 4
  • 30
  • 45