【发布时间】:2014-12-06 13:08:46
【问题描述】:
我一直在阅读Teach Yourself C++ In 21 Days , by Jesse Liberty一书,遇到了Exceptions and Errors一章。作者使用了这个例子:
int top = 10;
int bottom = 0;
try
{
cout << "top divided by bottom = ";
cout << (top / bottom) << endl;
}
catch(...)
{
cout << "something has gone wrong!" << endl;
}
这对他来说很好用。异常被捕获,输出为:
top divided by bottom = something has gone wrong!
我自己试了一下,收到如下错误:
Unhandled exception at 0x0124152d in stuff.exe: 0xC0000094: Integer division by zero.
根据this thread,“整数除以零在标准 C++ 中也不例外。”但是 Visual Studio 显然在这里抛出了一个异常,虽然没有捕捉到它。
我尝试将top 和bottom 定义为double,我收到以下输出:
top divided by bottom = 1.#INF
那么为什么try catch 块没有捕获integer division by zero 异常?
【问题讨论】:
-
这太糟糕了。这是未定义的行为(这不是 C++ 异常)。我知道这本书很糟糕,但这太荒谬了。
-
这并不能激发人们对那本书的信心。
-
Windows 异常和 C++ 异常是非常不同的野兽。我建议烧掉那本书和getting a better one。不要只是把它扔掉 - 其他人可能会捡起它。
-
try和catch仅用于 C++ 异常而不是 CPU 异常,它们是两个正交的概念,尽管名称相似,但完全不同。 -
应该有一本书叫《21年自学C++》。
标签: visual-c++ exception-handling seh