CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  1. #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

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