我编写了自己的异常类,派生自std::runtime_error
以获得错误ID,时间戳和内部异常。
它似乎有效,但有任何缺点吗?
我唯一看到的是复制构造函数中的深层复制,当存在许多嵌套异常时,它是无效的。但例外情况应该是罕见的,而不是嵌套太多,所以我认为这是我可以应对的缺点。
#pragma once
#include <stdexcept>
#include <string>
#include <sstream>
#include <time.h>
#include <memory>
class MyException : public std::runtime_error
{
public:
MyException(const MyException& exception)
: std::runtime_error(exception.what()),
exceptionId(exception.id()),
ts(exception.timestamp()),
innerException(NULL)
{
if (exception.inner() != NULL)
{
innerException = new MyException(*exception.inner());
}
else
{
innerException == NULL;
}
}
MyException(const std::string& _Message)
: std::runtime_error(_Message),
exceptionId(0),
innerException(NULL)
{
time(&ts);
}
MyException(const std::string& _Message, unsigned int id)
: std::runtime_error(_Message),
exceptionId(id),
innerException(NULL)
{
time(&ts);
}
MyException(const std::string& _Message, unsigned int id, MyException* innerException)
: std::runtime_error(_Message),
exceptionId(id),
innerException(new MyException(*innerException))
{
time(&ts);
}
virtual ~MyException()
{
delete innerException;
}
unsigned int id() const { return exceptionId; }
time_t timestamp() const { return ts; }
const MyException* inner() const { return innerException; }
private:
unsigned int exceptionId;
time_t ts;
const MyException* innerException;
};
我将如何使用它:
void handleException(MyException *ex)
{
cout << "exception " << ex->id() << " - " << ex->what() << endl;
const MyException* innerException = ex->inner();
int t = 1;
while (innerException != NULL)
{
for (int i=0; i<t; ++i)
{
cout << "\t";
}
++t;
cout << "inner exception " << innerException->id() << " - " << innerException->what() << endl;
innerException = innerException->inner();
}
}
void throwRecursive(int temp)
{
if (temp == 0)
{
throw runtime_error("std::runtime_error");
}
try
{
throwRecursive(--temp);
}
catch (MyException &ex)
{
throw MyException("MyException", (temp+1), &ex);
}
catch (exception &ex)
{
throw MyException(ex.what(), (temp+1));
}
}
void myExceptionTest()
{
try
{
throwRecursive(3);
}
catch (MyException &ex)
{
handleException(&ex);
}
}
输出:
exception 3 - MyException
inner exception 2 - MyException
inner exception 1 - std::runtime_error