添加默认参数使此构造函数成为默认构造函数

时间:2013-01-12 16:51:42

标签: c++ arguments default

我正在尝试运行以下简单代码,但不断获取:

libc++abi.dylib: terminate called throwing an exception

有些人可以向我解释一下这意味着什么吗?

代码:

int main()
{
    ifstream in;
    cout << "Enter name: ";
    string s = GetLine();
    in.open(s.c_str());
    if (in.fail())
        Error("Error your file was not found");
    return 0;
}

错误来自以下内容:

ErrorException::ErrorException(string m="unspecified custom error") 
: msg(m) {
}

ErrorException::~ErrorException() throw() {}

const char* ErrorException::what() const throw() {
return this->msg.c_str(); 
}

void Error(string str) {
    ErrorException err(str);
    throw err;
}

我应该找回我指定的错误消息,但我没有;谁能明白为什么?

1 个答案:

答案 0 :(得分:1)

你抛出一个你没有抓住的例外。这终止了该计划。您没有代码可以接收错误消息,打印它或执行类似的操作。如果要捕获异常,请使用try / catch块。在catch部分,您可以使用错误消息执行任何操作。

尝试类似:

int main()
{
    ifstream in;
    cout << "Enter name: ";
    string s = GetLine();
    try
    {
       in.open(s.c_str());
       if (in.fail())
           Error("Error your file was not found");
    }
    catch (ErrorException& e)
    {
       cerr << e.what() << endl;
    }
    return 0;
}
相关问题