0

I've tried about 20 attempts & read numerous pages for the last 2 hours and can't figure out what I'm doing wrong here:

#pragma once
#include <exception>
using namespace std;

class EmptyHeap : public exception {
public:
    virtual const char* what() const throw()
    {
        return "The heap is empty!";
    }
};

Then in the heap class, a public method:

void remove() throw()//EmptyHeap
{
    if (isEmpty())
    {
        EmptyHeap broken;
        throw broken;
    }
    ...

This code works, but the original header was:

void remove() throw EmptyHeap;

Is there a way to specify what exception a method throws in C++, or is that just a Java thing?

Joseph Schmidt
  • 119
  • 1
  • 10
  • 4
    [The correct syntax](http://en.cppreference.com/w/cpp/language/except_spec) is `throw(EmptyHeap)` (note parentheses). Note further that exception specifications are deprecated. Just drop it. – Igor Tandetnik Dec 02 '16 at 15:21
  • [C++ exception specification replacement](http://stackoverflow.com/q/14609329/669576) – 001 Dec 02 '16 at 15:22
  • 1
    Note that this should very likely not be an exception at all. How would you "recover" from an `EmptyHeap` exception? It's a programming error to remove something from an empty heap, so the correct thing to do would be to add a precondition for `isEmpty`. Which means you document the precondition for users of the class and put an `assert(!isEmpty());` in the code. – Christian Hackl Dec 02 '16 at 16:08
  • 1
    Also note that C++ exception specification wasn't very well supported on all compilers (\*coughVisualStudiocough\*), even before it was deprecated. – Justin Time - Reinstate Monica Dec 02 '16 at 23:33

1 Answers1

1

Is there a way to specify what exception a method throws in C++, or is that just a Java thing?

Yes there is, and yes it's a java thing that's extremely unwelcome in any c++ program. If the function can throw an exception, just leave the exception specification blank. If it must not, use noexcept (>= c++11) or throw() (< c++11)

In addition, you can help yourself a lot by deriving any user exception from either std::runtime_error or std::logic_error (or any of the other standard errors).

e.g.

#include <stdexcept>

// this is literally all you need.
struct EmptyHeap : std::logic_error {
    // inherit constructor with custom message
    using logic_error::logic_error; 

    // provide default constructor
    EmptyHeap() : logic_error("The heap is empty") {}
};

now throw with either:

throw EmptyHeap();

or with a custom message:

throw EmptyHeap("the heap is really empty");
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • 1
    That "Java thing" is called "checked exceptions" and actually tends to be rare in good Java code as well. See Item 41 in Joshua Bloch's Effective Java book ("Avoid unnecessary use of checked exceptions"). – Christian Hackl Dec 02 '16 at 16:05
  • That pretty much confirms my suspicions that the teacher is coming from a Java background, the student I'm working with keeps coming to me with these C++ assignments that don't feel like C++ assignments. – Joseph Schmidt Dec 04 '16 at 06:24
  • @JosephSchmidt that is a terrible shame for the students. – Richard Hodges Dec 04 '16 at 09:44