【发布时间】:2015-01-26 13:02:44
【问题描述】:
我的代码有一个小问题。出于某种原因,当我尝试使用下面的代码抛出一个字符串时,我在 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;
}
}
错误:
【问题讨论】:
-
如果你想捕获一个字符串,抛出一个字符串而不是字符串文字。否则捕获一个 const char*。尽管使用其中一个异常类会更好。
标签: c++ exception error-handling