Derive all of your exceptions from a common base class BaseException
that has a virtual method GetMessage()
.
Then catch(const BaseException& e)
.
Jesse Beder
2010-03-25 03:39:05
Derive all of your exceptions from a common base class BaseException
that has a virtual method GetMessage()
.
Then catch(const BaseException& e)
.
You should create a base exception class and have all of your specific exceptions derive from it:
class BaseException { };
class HourOutOfRangeException : public BaseException { };
class MinuteOutOfRangeException : public BaseException { };
You can then catch all of them in a single catch block:
catch (const BaseException& e) { }
If you want to be able to call GetMessage
, you'll need to either:
BaseException
, orGetMessage
a virtual member function in BaseException
and override it in each of the derived exception classes.You might also consider having your exceptions derive from one of the standard library exceptions, like std::runtime_error
and use the idiomatic what()
member function instead of GetMessage()
.