谁能告诉我为什么程序在try块中调用abort()?

JDe*_*ein 0 c++

下面的程序编译成功,但它无法运行并调用abort()函数,该函数抛出一条消息,警告"此应用程序已请求Runtime以不寻常的方式终止它.请在应用程序的支持团队中获取更多信息." , 为什么这样?

#include<cstring>
#include<iostream>

using std::string;
using std::endl;
using std::cout;

class ThrowException{

    private:
        string msg;
        int b;
    public:
        ThrowException(string m="Unknown exception",int factor=0) throw(string);        //A

};

ThrowException::ThrowException(string m, int f) throw(string):msg(m),b(f){                 //B
    if(b==1)
        throw "b=1 not allowed.";
}

int main(){

    try{
        ThrowException a("There's nothing wrong.", 1);
    }catch(string e){
        cout<<"The address of e in catch block is "<<&e<<endl;
    }    

}
Run Code Online (Sandbox Code Playgroud)

错误信息

Mic*_*zek 7

在这一行:

throw "b=1 not allowed."
Run Code Online (Sandbox Code Playgroud)

你真的扔了一个const char*.如果您将其更改为:

throw std::string("b=1 not allowed.")
Run Code Online (Sandbox Code Playgroud)

或者将catch块(和相应的throw限定符)更改为:

}catch(const char* e){
Run Code Online (Sandbox Code Playgroud)

它会工作