如果参数不适合格式化,我想抛出消息。我实际上不知道会发生什么。我试图用std :: string抛出消息,但是不起作用。那里发生了什么事?
另外,编译器给出了“中止调用”消息。
我在打开主题之前先进行了搜索,但是找不到满意的答案。
string timeConversion(string s) {
// hh:mm:ss:AM
if(s.length() != 11)
throw "message";
return something;
}
编辑:简化代码。
不要抛出字符串(const char*
):
throw "failed because of hour";
例外class
:
class MyException : public std::exception
{
public:
MyException(const char* err) : std::exception(err) {}
};
并且throw
:
throw MyException("failed because of hour");
然后catch
:
try
{
// code that might throw
}
catch(const MyException& ex)
{
// process exception
}
如果引发了异常但没有捕获到异常(您当前的情况),则terminate
调用该异常。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句