我试图演示异常处理,但遇到了一个我无法解决的奇怪问题。问题出现在以下代码中:
#include <iostream>
#include <cmath>
#include <stdexcept>
using namespace std;
double sqrt(double x) {
if ( x < 0 ){
throw invalid_argument("sqrt received negative argument");
}
return sqrt(x);
}
int main(int argc, char *argv[]) {
try {
double s = sqrt(-1);
}
catch (const exception& e) {
cout << "Caught " << e.what() << endl;
}
return 0;
}
代码失败:
terminate called after throwing an instance of 'std::invalid_argument'
what(): sqrt received negative argument
./dostuff.sh: line 8: 3472 Aborted (core dumped) ./may_22.exe
但是,如果我将写入的 sqrt 函数的名称更改为“mySqrt”,或者删除标头,则可以正确捕获异常。知道是什么原因造成的吗?
我正在编译通过
g++ -g -Wall -std=c++0x -Weffc++ may_22.cpp -o may_22.exe
和g++ (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
编辑:澄清一下,这似乎不是命名空间问题。代码显然在调用我的 sqrt 函数,如异常消息所示。
编辑 2:此代码仍然无法为我处理异常。
#include <iostream>
#include <cmath>
#include <stdexcept>
double sqrt(double x) {
if ( x < 0 ){
throw std::invalid_argument("sqrt received negative argument");
}
return std::sqrt(x);
}
int main(int argc, char *argv[]) {
try {
double s = sqrt(-1);
}
catch (std::exception& e) {
std::cout << "Caught " << e.what() << std::endl;
}
return 0;
}