2

I'm currently building a C++ game engine as a learning exercise, and am incorporating exceptions into the less performance-critical sections. I'm primarily a PHP and Ruby developer, so I'm used to declaring new classes of exception on a regular basis using simple syntax like this:

class SomeSubSystemException < Exception; end

or

class SomeSubSystemException extends Exception {};

is there an easy syntax for doing this in C++, or am I going about exception handling the wrong way for C++ projects? Currently I have to do the following for every class of exception, which makes me want to not define very many:

class SubSystemException : public MainException {
    SubSystemException(std::string& msg) : MainException(msg) {}
};

Thanks in advance!

4
  • You can just derive from std::exception.
    – Alok Save
    Commented Dec 8, 2012 at 13:31
  • I should clarify, in this example MainException is derived from std::runtime_error in the same way that SubSystemException is derived from MainException Commented Dec 8, 2012 at 13:32
  • What's the problem? That you have to write two lines instead of one? Defining new exceptions will not be the main part of your work, will it?
    – Bo Persson
    Commented Dec 8, 2012 at 14:10
  • try avoiding Exceptions. informit.com/guides/content.aspx?g=cplusplus&seqNum=266
    – whunmr
    Commented Dec 8, 2012 at 14:31

2 Answers 2

3

Define a Macro that does that for you.

#define NEW_EXC(Derived, Base) class Derived : Base {\
 Derived(const std::string& msg) : Base(msg) {}

NEW_EXC(SubSystemException, MainException);

#undef NEW_EXC

Done.

Beware macros can be evil.

4
  • Instead of three lines of code, there is now four. Great! :-)
    – Bo Persson
    Commented Dec 8, 2012 at 14:12
  • @BoPersson Yes for just one new Exception this is not very efficient but he said he is defining MANY exceptions...
    – RedX
    Commented Dec 8, 2012 at 14:14
  • I think he has overestimated the amount of exceptions he will have to declare. In a non-trivial game engine there will be tons of code to write. Exceptions will not be the main problem. Besides, with the macro he will get the semicolon wrong half the time (is it part of the macro or the use). :-)
    – Bo Persson
    Commented Dec 8, 2012 at 14:18
  • @BoPersson you're right, writing exceptions won't be my biggest problem, but I have a severe allergy to boilerplate code and am trying to make my project as readable as possible so I can drop it and pick it up as I please :) Commented Dec 8, 2012 at 14:33
1

Although I agree with the comment from Bo Person saying that this will not be your main source of boilaerplate, here another way for the heck of it:

Use CRTP

template<class T>
class MainException : public std::runtime_error{
private:
  MainException(std::string const& msg):std::runtime_error(msg){ }
public:
  static MainException<T> create(std::string const& msg){
    return MainException<T>(msg);
  }
};

//Usage:
class MyException : public MainException<MyException> {};
void foo(){
  MyException::create(std::string("Foo"));
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.