在字符串函数中抛出'char const*'实例后调用的终止
terminate called after throwing an instance of 'char const*' in string function
如果参数不适合格式化,我想抛出消息。我其实不知道会发生什么。我试图用 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
。
相关文章:
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 从基类实例调用派生类方法而不进行强制转换
- 如何在C++从父实例调用子函数
- 为 std::shared_ptr 实例调用 Operator()
- 跨类的多个实例调用函数一次
- 如何从派生类实例调用父类重载函数
- 如何在C++中跨实例调用方法
- 防止同一类的实例调用公共功能(C )
- 使用Objective C类实例调用C函数
- 从结构实例调用变量语法问题C++
- 从基类实例调用派生类方法
- C++从基类实例调用派生函数
- 是否可以从不存在的实例调用方法?
- 如何使用派生类实例调用基类重载的func,
- 为singleton实例调用函数创建一个别名
- 用结构体的所有实例调用函数
- 从继承的类实例调用静态成员
- 当我从类实例调用方法时,我的C程序崩溃了,下面是代码
- 当尝试使用类的实例调用方法时,调试断言错误
- C++:从派生实例调用 base 中的纯虚拟方法重载