【发布时间】:2009-09-23 23:44:24
【问题描述】:
我在业余时间玩弄 C++ 代码,致力于构建一个可用于构建 RPG 类型游戏的通用库。它主要关注底层数据结构和功能。我离处理图形还有很长的路要走。
我的问题是:异常应该有多复杂。我有一个看起来像这样的基本例外:
标题:
#include <iostream>
class Exception
{
protected:
std::string messageM;
public:
Exception() throw();
Exception(std::string message) throw();
Exception(std::string file, int line) throw();
Exceptoin(Exception & exception) throw();
virtual ~Exception();
virtual std::string getMessage(void) throw();
virtual void setMessage(std::string message) throw();
};
构造函数大多只是一堆“messageM = message”之类的东西。
Exception(std::string, int) 使用 stringstream 将数字转换为字符串。
这太复杂了吗?
我希望平均异常调用类似于:
Exception exception("Message Here");
throw exception;
或
Exceptoin exception(__FILE__, __LINE__);
throw exception;
另外,在相关说明中,C++ 是否允许您创建异常并同时抛出它。类似于throw(Exception exception("My Message")); 的事情似乎给了我错误。
^_^ 谢谢。
【问题讨论】: