VS C++在特定检查后抛出被零除异常

发布于 2024-08-26 23:21:38 字数 706 浏览 4 评论 0原文

在以下 C++ 代码中,应该不可能发生整数除以零的情况:

// gradedUnits and totalGrades are both of type int
if (gradedUnits == 0) {
    return 0;
} else {
    return totalGrades/gradedUnits; //call stack points to this line
}

但是 Visual Studio 会弹出此错误:

DSA_asgn1.exe 中 0x001712c0 处未处理的异常:0xC0000094:整数除以零。

堆栈跟踪指向代码中指示的行。

更新:我可能只是在这里做了一些愚蠢的事情。在尝试让 VS 关注我的调试断点时,我重建了解决方案,并且异常不再发生。在我看来,当我认为我正在开始新的会话时,我可能在调试会话中停止并恢复它。

感谢您的回复。由于我的问题已经解决并且并不是我想象的那样,因此删除我的问题是否合适?

看起来 VS 可能只是对任何整数除法执行此操作,而不检查是否可以除以零。即使代码永远不能抛出该异常,我是否需要捕获该异常?如果是这样,最好的方法是什么?

这是针对指定 VS 2005/2008 with C++ 的作业。我不想让事情变得比我需要的更复杂,但同时我喜欢尽可能正确地做事。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清君侧 2024-09-02 23:21:38

您应该尝试使用 VS 调试器浏览此代码,并查看这些变量的实际值是什么。

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

三生殊途 2024-09-02 23:21:38

事实证明,这个问题是由我最初拥有的代码引起的,该代码没有除零检查,如下所示:

return totalGrades/gradedUnits;

问题是,虽然我更新了代码,但实际上仍然处于同一个调试会话中这引发了原始错误,因此程序仍在旧代码上运行,并且每次重新启动时都会再次引发错误。

通过重建解决方案解决了该问题,这强制了新的调试会话。只需终止调试会话并通过重建重新启动它也可以解决这个问题(我只是没有注意到我仍在会话中)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文