I wrote some classes that don't throw exceptions but they use the STL and the STL can throw exceptions. For example in my class there are functions that use std::vector, std::list, std::string. The STL could throw when copying a string or creating a vector, right? So I cannot describe my classes as exception free, right?
What do you guys do in a circumstance like this? Do you wrap every function in a try/catch? How do you describe your class? Thanks
Correct, if anything you call from a particular member function (including constructors, implicit calls that the compiler provides for you, etc) can throw an exception, then the member function can throw an exception. So it's not exception free.
As to what to do about it: It really depends on what your code is supposed to do, and "what can you do if it throws an exception". You probably want to catch it SOMEWHERE, but since most likely scenario is either that you've done something daft and/or run out of memory, you will most likely not really be able to do much about the situation. (Of course, if you use for example std::vector::at()
with an out-of-range value, then it will throw an exception - that's "doing something daft" - likewise the one I've done a couple of times, const char* p = 0; .... std:string str(p);
- it may of course crash rather than throw an exception, but my compiler seems to throw a bad_something exception from this). Any of these things, if they are not intended, is probably "the death of your code anyway". If you are using std::vector::at()
with a bad index and you "intended it", then you probably shoul rethink your design - exceptions are "expensive" compared to if (vec.size() > index) ... else ...
I'm not sure if there is a specific term for "my class does not throw exceptions, but uses standard library that may do".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With