3

Hi I have a c++ assignment in which I need to create my own exceptions. My exception class has to inherit from the std::exception and 2 other classes need to derive from that one. The way I have it right now it actually compiles and works pretty much ok. But when the exception is thrown I get e.g.: terminate called after throwing an instance of 'stack_full_error' what(): The stack is full! Aborted (core dumped)

I am very confused about this matter and unfortunately I could not find much help on the web or in my books. My header is something like the following:

class stack_error : public std::exception
{
 public:
  virtual const char* what() const throw();
  stack_error(string const& m) throw(); //noexpect;
  ~stack_error() throw();
  string message;
 private:
};

class stack_full_error : public stack_error
{
 public:
  stack_full_error(string const& m) throw();
  ~stack_full_error() throw();
  virtual const char* what() const throw();
};

class stack_empty_error : public stack_error
{
 public:
  stack_empty_error(string const& m) throw();
  ~stack_empty_error() throw();
  virtual const char* what() const throw();
};

and my implementation is:

stack_error::stack_error(string const& m) throw()
: exception(), message(m)
{
}

stack_error::~stack_error() throw()
{  
}

const char* stack_error::what() const throw()
{
  return message.c_str();
}

stack_full_error::stack_full_error(string const& m) throw() 
  : stack_error(m)
{   
}

stack_full_error::~stack_full_error() throw()
{

}

const char* stack_full_error::what() const throw()
{
  return message.c_str();
}

stack_empty_error::stack_empty_error(string const& m) throw() 
  : stack_error(m)
{   
}

stack_empty_error::~stack_empty_error() throw()
{

}

const char* stack_empty_error::what() const throw()
{
  return message.c_str();
}

any help would be greatly appreciated!

4
  • 1
    Are you catching the exception? Commented Jan 7, 2014 at 23:07
  • 1
    The code you posted looks mostly working: the constructors should all have the throw() specification removed because each one of them actually can throw an exception! I don't think there is a reason to make the std::string a public member, either, and it is surely sufficient to override what() just in the base class. Can you show the code where you actually catch the exception? Commented Jan 7, 2014 at 23:09
  • Please consider using virtual inheritance when creating your own exception types. Also, if stack_error inherits from runtime_error instead of exception you won't need the string member variable. Commented Jan 7, 2014 at 23:24
  • Thank you very much. It seems that I had forgotten to change my "catch" and I was still looking to catch an std::out_of_range. Commented Jan 7, 2014 at 23:42

1 Answer 1

3

You need to actually catch your exception:

try
{
    throw stack_full_error("Stack is full!");
} catch(stack_full_error& ex)
{
    std::cout << ex.what();
}
Sign up to request clarification or add additional context in comments.

12 Comments

Whoever invented exceptions isn't very high on my list of most respected people. Why can't there just be a return value that indicates something happened?
@BWG This would require hacky return types. You can provide detailed information about the error using exceptions. Moreover, you can catch any exceptions without explicitly checking through all possible ones.
@BWG: Return codes return an error locally -- exceptions can move up the stack. The guts of your algorithm probably can't deal with an out of memory situation, but the upper levels of the app may be able to.
@BWG: Exceptions are for exceptional situation. Return values are for normal operation. In many cases a return value is appropriate, namely when dealing with some failure state is a fundamental part of the way you use the interface. Exceptions are for the things you don't want to deal with right there and then.
@BWG Interestingly, the Linux kernel and many other C code bases use goto for error handling in order to jump to cleanup code. And this is considered best practice in C whereas in C++ with exceptions your cleanup is done automatically by destructors of local objects.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.