为什么叫中止方法?

Suh*_*pta 6 c++ exception

在下面的程序中,abort即使我有适用的catch语句,也会调用该方法.是什么原因?

#include <iostream>
#include <string>
using namespace std;

int main() {

    try {
        cout << "inside try\n";
        throw "Text";
    }
    catch (string x) {
        cout << "in catch" << x << endl;
    }

    cout << "Done with try-catch\n";
}
Run Code Online (Sandbox Code Playgroud)

当我运行程序时,我只inside try显示第一个语句,然后我收到此错误:

在此输入图像描述

为什么abort即使在我处理string异常时也会被调用?

Lig*_*ica 14

真的很简单!

你扔了char const*,但没有匹配catch它.

你的意思是throw std::string("...");

  • @Suhail:不."......"`是一个"字符串文字",一个`char`s数组.`std :: string`对象不会神奇地显示出来:你必须创建它们.您可能会对`std :: string`可以从`char const*`构造的事实感到困惑,因此转换在函数调用期间自动发生.但是当你写`void f(const std :: string&); f("lol");`,`"lol"`是_not_一个`std :: string` ..由于隐式转换魔法,它只是变成一个.对于例外而言,这不会发生. (3认同)
  • 当你在这里时,请参考. (2认同)