C/C++ Is Deriving from std::exception Safe in C++?

Click For Summary
Deriving from STL classes is generally discouraged due to the lack of virtual destructors, which can lead to issues down the line. However, deriving from std::exception and std::runtime_error is considered acceptable since they do have virtual destructors. The discussion highlights the intention to create a custom exception that can store multiple warnings or errors using a std::vector or other STL containers. The practice of overriding the ostream operator for easier output is also mentioned as a potential benefit. The consensus suggests that while caution is advised, deriving from std::exception aligns with its intended use, and there are existing examples of specific exception subtypes that support this approach. The contributor aims to clarify these practices in a guide for those transitioning from C to C++.
sbrothy
Gold Member
Messages
1,386
Reaction score
1,239
I'm not a newbee to C++ but I've been out of the loop for a while.

I know it's discouraged, if not directly a no-go, to derive from STL classes. Something that has to do, if I remember correctly, with the STL classes having no virtual destructors.

It will work in the short run but may explode in your face later if you don't know exactly what you're doing.

With std::exception (and std::runtime_error), however, which does actually have virtual destructor(s) this practice should be OK though, right?

Regards.
 
Technology news on Phys.org
  • Like
Likes Vanadium 50 and sbrothy
I may have unintentionally muddied the waters here. I didn't mean deriving from STL classes (which I'm pretty sure is frowned upon) but from std::exception (or possibly std::runtime_error, but in my scenario I don't need the what() string that comes with the last). I want to store a std::vector (or some other STL container) inside my std::exception derivative because my custom exception needs to store a bunch of warnings or errors. It seems to be widely used and I can't really see any downsides which outweighs the potential benefits.

I can even override the ostream<< operator for easy output. For output to std::cerr and DEBUG output.

I'll post my std::exception derivative one I've cooked it up. You are welcome to shoot it down by then. :)
 
@sbrothy, I don't see any downside to this. After all, there are already several specific exception subtypes that derive from exception class. These include the classes declared in the STL <stdexception> header, including logic_error, domain_error, runtime_error, range_error, and overflow_error.
 
  • Like
Likes sbrothy
Good. As I pointed out I may have mixed up deriving from STL containers with deriving from std::exceptions. After all it looks like what the the std::exception is meant for. Virtual constructor and all. But as I mentioned I've been out of the look for a while so just wanted to be sure. I want to write a little C++ guide for people coming from C, so naturally I didn't want to put my foot in it right away. The way I want to derive from std::exception seems pretty "by design" and frankly I can see no better way.

I'll keep you updated...
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 118 ·
4
Replies
118
Views
9K
Replies
5
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
2
Views
7K
  • · Replies 18 ·
Replies
18
Views
4K
Replies
3
Views
3K