CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2012
    Location
    Westchester, NY
    Posts
    2

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    Last edited by laserlight; July 9th, 2012 at 01:18 PM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jul 2012
    Location
    Westchester, NY
    Posts
    2

    Re: Subclassing from std::exception

    Quote Originally Posted by laserlight View Post
    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?

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Subclassing from std::exception

    Quote Originally Posted by RobWelch View Post
    [...] 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.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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
  •  





Click Here to Expand Forum to Full Width

Featured