【发布时间】:2010-06-24 21:11:49
【问题描述】:
使用以下代码,我得到了“Gotcha!”用python。
尝试: x = 0 y = 3/x 除了例外: # ZeroDivisionError 打印“明白了!”我认为这是等效的 C++ 代码,但它无法捕捉到异常。
#include <iostream>
int main()
{
int x = 0;
//float y = 3.0/x;
int z = 0;
try {
z = 3 / x;
} catch (std::exception) {
std::cout << "Gotcha!";
}
std::cout << z;
}
浮点异常
出了什么问题? 我怎样才能捕捉到这个异常?
【问题讨论】:
-
对于 Windows 特定的观点,请参阅这个问题:stackoverflow.com/questions/1909967/…