Subclassing from std::exception
Hi:
The standard approach to subclassing from std::exception seems to be to override the virtual what() & provide your own storage for the returned error message. However, I've found that I can also get the functionality I want by using the std::exception ctor that takes a string to set the error message & then I don't need to override what(). This seems to me to be preferable, so I'm wondering why this isn't the standard approach to subclassing std::exception. Is there a pitfall to this approach that I'm missing?
Thanks in advance!
Re: Subclassing from std::exception
I prefer to subclass either std::logic_error or std::runtime_error from <stdexcept>, upon which you can simply write something like:
Code:
class MyException : public std::runtime_error
{
public:
MyException() : std::runtime_error("My error message.") {}
};
EDIT:
Quote:
Originally Posted by RobWelch
However, I've found that I can also get the functionality I want by using the std::exception ctor that takes a string to set the error message & then I don't need to override what(). This seems to me to be preferable, so I'm wondering why this isn't the standard approach to subclassing std::exception. Is there a pitfall to this approach that I'm missing?
There is no such constructor for std::exception. If there is for you, then you're using an extension to the standard library, so it is non-standard.
Re: Subclassing from std::exception
Quote:
Originally Posted by
laserlight
I prefer to subclass either std::logic_error or std::runtime_error from <stdexcept>, upon which you can simply write something like:
Code:
class MyException : public std::runtime_error
{
public:
MyException() : std::runtime_error("My error message.") {}
};
EDIT:
There is no such constructor for std::exception. If there is for you, then you're using an extension to the standard library, so it is non-standard.
Sorry, I was imprecise. I do not have a std::exception constructor that takes a string, but I do have one declared as follows:
__CLR_OR_THIS_CALL exception(const char *const&);
Is this nonstandard (I'm using VS 2008, so I wouldn't be surprised if it were.)? I would be very surprised if there were no standard std::exception ctor that takes a const char *; how else is the string returned by std::exception::what() set?
Re: Subclassing from std::exception
MS often add things that are non-standard. It might be good or it might be bad depending on what platform(s) you target.
If you're concerned about portability always check things up on MSDN http://msdn.microsoft.com/en-us/library/c4ts6d5a.aspx.
Re: Subclassing from std::exception
Quote:
Originally Posted by
RobWelch
[...] I do not have a std::exception constructor that takes a string, but I do have one declared as follows:
__CLR_OR_THIS_CALL exception(const char *const&);
The CLR in the name of the macro apparently specifying the linkage/calling convention together with the fact that this c'tor is MS-specific, according to the link posted by S_M_A, seems to indicate that this c'tor is an attempt by MS to create some fusion between std::exception and the .NET System::Exception which does have a c'tor with that signature, assuming there's an implicit conversion from const char * to System::String ^ which at least is the case in C++/CLI. I have no concrete idea of what practical use that might be, but that's what the plot looks like to me.
Re: Subclassing from std::exception
Quote:
Originally Posted by RobWelch
I would be very surprised if there were no standard std::exception ctor that takes a const char *; how else is the string returned by std::exception::what() set?
Notice that what() is a virtual member function; subclasses can override what() to return a pointer to the first character of a null terminated string obtained from say, a std::string.
Re: Subclassing from std::exception
Rule of thumb:
*If it starts with an underscore, then it is not standard.
*If it starts with 2 underscores, then it is not standard, and compiler specific.