Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

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.

added 27 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

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.

Rollback to Revision 3
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

###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;
};
added 1525 characters in body
Source Link
Ben
  • 161
  • 1
  • 4
Loading
Post Merged (destination) from codereview.stackexchange.com/questions/16359/…
Tweeted twitter.com/#!/StackCodeReview/status/255684504730079232
deleted 1 characters in body
Source Link
Ben
  • 161
  • 1
  • 4
Loading
edited title
Link
Ben
  • 161
  • 1
  • 4
Loading
Source Link
Ben
  • 161
  • 1
  • 4
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /