我的代码有一个小问题。出于某种原因,当我尝试使用下面的代码抛出一个字符串时,我在 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;
}
}
错误 :
原文由 Jimmy 发布,翻译遵循 CC BY-SA 4.0 许可协议
您目前正在抛出
const char*
而不是std::string
,而是应该抛出string("error")
编辑:错误已解决