-
July 9th, 2012, 01:07 PM
#1
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!
-
July 9th, 2012, 01:11 PM
#2
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:
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.
Last edited by laserlight; July 9th, 2012 at 01:18 PM.
-
July 9th, 2012, 01:46 PM
#3
Re: Subclassing from std::exception
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?
-
July 9th, 2012, 03:05 PM
#4
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.
-
July 9th, 2012, 08:11 PM
#5
Re: Subclassing from std::exception
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.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
July 9th, 2012, 09:34 PM
#6
Re: Subclassing from std::exception
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.
-
July 10th, 2012, 01:03 AM
#7
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.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|