0

以下のプログラムは正常にコンパイルされますが、実行に失敗し、abort() 関数を呼び出すことができず、「このアプリケーションはランタイムに通常とは異なる方法で終了するように要求しています。詳細については、アプリケーションのサポート チームにお問い合わせください。」という警告メッセージがスローされます。 、 なんでそうなの?

#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;
    }    

}

エラーメッセージ

4

1 に答える 1

7

この行で:

throw "b=1 not allowed."

あなたは実際にを投げていconst char*ます。次のように変更した場合:

throw std::string("b=1 not allowed.")

または、catchブロック(および対応するthrow修飾子)を次のように変更します。

}catch(const char* e){

それが動作します

于 2012-05-01T06:02:00.710 に答える