C++
allows throw
ing any kind of objects. From exception
s to string
and even int
.
But I've never seen any real world application of throw
ing anything other than exception
s.
My question is, what is the application for throw
ing non-exception
objects?
From a viewpoint of practicality, there is almost1 no application for throwing string
s, int
s, or anything else that isn't derived from std::exception
.
This isn't because there's no indication for doing so, but because there are contra-indications that suggest why you shouldn't.
There are two main reasons why you wouldn't want to throw anything that's not derived from std::exception
:
std::string
and the construction or copy of that string
raises another exception, terminate
will be called and your process will cease to exist. You'll never get a chance to catch
that std::string
. std::exception
makes it possible to catch (const std::exception&)
in a generic fashion. If you throw something else, you will need a catch
for that case.A good discussion of exceptions can be found here.
1 Almost no application [...]: There are exclusions to every rule, but even in acknowledging this, I have never seen a legitimate exclusion to throwing a derivitave of std::exception
.
More of a hack rather than a language feature, you could throw an object and then catch it to force a function to "return" something different than its normal return type.
int aFunc()
{
throw foo(); // if you catch that foo, you 've imitated returning foo
return 0; // ok just an int
}
This would ofcourse be a terrible design choice and a violation of type safety offered by C++ but say you have a function heavily used in a huge code base and you want to try some change (which involves altering the return type) then that would be dirty way of trying something out before actually implementing the change (and grep the whole code base to do changes)
EDIT:
You should read the post more carefully. I 've said "a terrible design choice" and "a violation of type safety offered by C++" and "before actually implementing the change". If that is not enough of a warning I don't think those comments or downvotes would be.
On the other hand try altering the return type of a function used 666 times in a code base of 6e06 lines to find out that it's not what you want after you've uploaded it on your version control system and broke the compile multiple times for developers working on other plattforms than you.
If there was a shortcut wouldn't you want to know about it? Wouldn't you use it until implementing the change and actually posting it to your code base?
Even if the answer to those questions is "NO" I thought this post was about exploring possibilities, and just mentioning one is not per se 'evil'. I personally heard this one from a Bjarne's talk http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Keynote-Bjarne-Stroustrup-Cpp11-Style who afterwards said the same things about not using such things.
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