Jim*_*mmy 8 c++ error-handling exception-handling exception
我的代码遇到了一个小问题.出于某种原因,当我尝试使用下面的代码抛出一个字符串时,我在visual studio中出错了.
#include <string>
#include <iostream>
using namespace std;
int main()
{
char input;
cout << "\n\nWould you like to input? (y/n): ";
cin >> input;
input = tolower(input);
try
{
if (input != 'y')
{
throw ("exception ! error");
}
}
catch (string e)
{
cout << e << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
错误:

Mr.*_*C64 14
扔一根绳子真是个坏主意.
随意定义一个自定义异常类,并在其中嵌入一个字符串(或者只是从中派生自定义异常类std::runtime_error,将错误消息传递给构造函数,并使用该what()方法在catch-site获取错误字符串),但是千万不能丢一个字符串!
Syn*_*ose 12
你现在正在投掷一个const char*而不是一个std::string,而你应该投掷string("error")
编辑:错误已解决
throw string("exception ! error");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21554 次 |
| 最近记录: |