I already found out here here that I'm violating the rule of three, having forgotten the copy assignment operator.
I already found out here that I'm violating the rule of three, having forgotten the copy assignment operator.
I already found out here that I'm violating the rule of three, having forgotten the copy assignment operator.
Is there anything wrong with my self Self-implemented C++ exception class?
I wrote my own exception class, deriving from std::runtime_error
to have Error-IDs, timestamps and inner exceptions.
It It seems to work, but are there any drawbacks?
And the output:
exception 3 - MyException
inner exception 2 - MyException
inner exception 1 - std::runtime_error
UPDATE
exception 3 - MyException inner exception 2 - MyException inner exception 1 - std::runtime_error
Other issues mentioned: cloning cloning, reserved names and throw specifications.
Is there anything wrong with my self-implemented C++ exception class?
I wrote my own exception class, deriving from std::runtime_error
to have Error-IDs, timestamps and inner exceptions.
It seems to work, but are there any drawbacks?
And the output:
exception 3 - MyException
inner exception 2 - MyException
inner exception 1 - std::runtime_error
UPDATE
Other issues mentioned: cloning, reserved names and throw specifications
Self-implemented C++ exception class
I wrote my own exception class, deriving from std::runtime_error
to have Error-IDs, timestamps and inner exceptions. It seems to work, but are there any drawbacks?
And the output:
exception 3 - MyException inner exception 2 - MyException inner exception 1 - std::runtime_error
Other issues mentioned: cloning, reserved names and throw specifications.
###Edit 1
I redesigned my exception class, following Loki Astaris advices. I'm now storing the inner exception in a std::shared_ptr
, which makes things simpler. No copy constructor, destructor or copy assignment operator needed anymore.
#pragma once // code doesn't have to be portable to other compilers
#include <stdexcept>
#include <string>
#include <ctime>
#include <memory>
class MyException : public std::runtime_error
{
public:
MyException(const std::exception& ex)
: std::runtime_error(ex.what()),
exceptionId(0)
{
time(&ts);
}
MyException(const std::string& message, unsigned int id = 0, std::exception* innerException = NULL)
: std::runtime_error(message),
exceptionId(id)
{
time(&ts);
if (innerException != NULL)
{
MyException *myex = dynamic_cast<MyException*>(innerException);
if (myex != NULL)
{
this->innerException = make_shared<MyException>(*myex);
}
else
{
this->innerException = make_shared<MyException>(MyException(*innerException));
}
}
}
unsigned int id() const { return exceptionId; }
time_t timestamp() const { return ts; }
const std::runtime_error* inner() const { return innerException.get(); }
private:
unsigned int exceptionId;
time_t ts;
std::shared_ptr<std::runtime_error> innerException;
};
###Edit 1
I redesigned my exception class, following Loki Astaris advices. I'm now storing the inner exception in a std::shared_ptr
, which makes things simpler. No copy constructor, destructor or copy assignment operator needed anymore.
#pragma once // code doesn't have to be portable to other compilers
#include <stdexcept>
#include <string>
#include <ctime>
#include <memory>
class MyException : public std::runtime_error
{
public:
MyException(const std::exception& ex)
: std::runtime_error(ex.what()),
exceptionId(0)
{
time(&ts);
}
MyException(const std::string& message, unsigned int id = 0, std::exception* innerException = NULL)
: std::runtime_error(message),
exceptionId(id)
{
time(&ts);
if (innerException != NULL)
{
MyException *myex = dynamic_cast<MyException*>(innerException);
if (myex != NULL)
{
this->innerException = make_shared<MyException>(*myex);
}
else
{
this->innerException = make_shared<MyException>(MyException(*innerException));
}
}
}
unsigned int id() const { return exceptionId; }
time_t timestamp() const { return ts; }
const std::runtime_error* inner() const { return innerException.get(); }
private:
unsigned int exceptionId;
time_t ts;
std::shared_ptr<std::runtime_error> innerException;
};