Should exception classes be part of the class which may throw them or should they exist on a higher level?
For example :
class Test
{
public:
class FooException: public ExceptionBase { };
void functionThrowingFooException();
};
or
class FooException: public ExceptionBase { };
class Test
{
public:
void functionThrowingFooException();
};
(functionThrowingFooException()
is the only function to ever throw a FooException
)
Exceptions really model error conditions. Are those specific to each class? What if you need to raise an exception from a free function?
If you go the route of providing different exception types for various problem states - analyze/list those error states, name exceptions after them, put whatever state-specific data into those exception types, derive from a subclass of std::exception
, then declare in a separate header.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With