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

  • Context: C/C++ 
  • Thread starter Thread starter sbrothy
  • Start date Start date
  • Tags Tags
    C++ Classes deriving
Click For Summary

Discussion Overview

The discussion revolves around the safety and appropriateness of deriving from std::exception in C++. Participants explore the implications of this practice, particularly in relation to the use of STL classes and the presence of virtual destructors. The conversation touches on theoretical considerations, practical applications, and personal experiences with exception handling in C++.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant notes that deriving from STL classes is generally discouraged due to the lack of virtual destructors, but questions whether deriving from std::exception is acceptable since it has a virtual destructor.
  • Another participant emphasizes the risks associated with following discouraged practices, suggesting that while some references may indicate it is acceptable, caution is advised.
  • A participant clarifies their intent to derive from std::exception specifically to store additional data (like warnings or errors) in their custom exception, arguing that this approach seems widely used and beneficial.
  • Another participant points out that there are existing specific exception subtypes derived from std::exception, indicating that such derivation is not uncommon.
  • A participant expresses a desire to create a guide for C programmers transitioning to C++, indicating that they want to ensure their approach to deriving from std::exception is sound.

Areas of Agreement / Disagreement

Participants express varying opinions on the safety of deriving from std::exception, with some supporting the practice and others cautioning against it. There is no consensus on whether it is entirely safe or advisable.

Contextual Notes

Some participants reference external sources for further information, but the discussion remains focused on personal insights and experiences rather than definitive conclusions. The conversation highlights the complexity and nuances involved in exception handling in C++.

Who May Find This Useful

Programmers interested in exception handling in C++, particularly those transitioning from C or exploring custom exception implementations.

sbrothy
Gold Member
Messages
1,495
Reaction score
1,365
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   Reactions: 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   Reactions: 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...
 

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
10K
  • · Replies 36 ·
2
Replies
36
Views
3K
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
2
Views
7K
  • · Replies 18 ·
Replies
18
Views
4K
Replies
3
Views
4K