自定义异常类 C++
Custom Exception Class C++
我会写一个自定义异常类,但这是不可能的,因为我有一个错误。
SSException.h :
#ifndef SSEXCEPTION_H
#define SSEXCEPTION_H
#include <iostream>
#include <exception>
class SSException: public std::exception {
public :
virtual const char* what() const throw();
private:
const char* errMessage_;
};
#endif
SSException.cpp :
using namespace std;
const char* SSException::what() const throw()
{
return "UNKNOW";
}
我会有一个自定义方法,例如:Virtual const char* getMsg(int code);
但是这个解决方案,行不通。你可以帮我吗?实现:
try {
st.timeRef();
} catch(const SSException& ex) {
//cerr << "Solar System Exception: n" << ex.what() << endl;
cerr << "Solar System Exception: n" << ex.getMsg(2) << endl;
}
错误:
SolarSystem.cpp:26:54:错误:将"const SSException"传递为"this" 'Virtual const char* SSException::getMsg(int)' 的参数丢弃 限定符 [-允许] cerr <<"太阳系异常:" << ex.getMsg(2) <<endl;
提前谢谢你
我会有一个自定义方法,例如:虚拟常量字符* getMsg(int code);
它需要virtual const char* getMsg(int code) const;
,因为你的代码通过不断的引用来捕获。在这种情况下const
仅表示函数getMsg
不会修改其对象。
相关文章:
- C++ 中的自定义异常:在哪里定义它们?
- 我应该C++自定义异常代码放在哪里?
- 自定义异常中的用户定义的空构造函数,具有多个继承和抽象基类
- 处理许多自定义异常的最佳方法是什么
- C++未显示自定义异常消息
- 自定义异常中的消息已损坏
- 为什么要创建自己的自定义异常类
- C++ 构造函数中的自定义异常抛出 ()
- JNI自定义异常具有多个参数
- 如何在抛出未处理的自定义异常后调用 what()?
- 无法创建两个从 std::logic_error 继承的自定义异常类
- 引发异常并直接结束程序的自定义异常处理
- 在C++中创建自定义异常
- G++ 自定义异常处理程序
- 自定义异常并不总是执行 what()
- 从boost::exception和std::runtime_error继承自定义异常类
- 添加到方法的 C++ 自定义异常会出错
- 在自定义异常中仍可访问(误报?
- 自定义异常类 C++
- 未捕获从std::异常派生的C++自定义异常