Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

throw non-exception objects

Tags:

c++

exception

C++ allows throwing any kind of objects. From exceptions to string and even int. But I've never seen any real world application of throwing anything other than exceptions.

My question is, what is the application for throwing non-exception objects?

like image 228
MBZ Avatar asked Feb 04 '14 17:02

MBZ


2 Answers

From a viewpoint of practicality, there is almost1 no application for throwing strings, ints, 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:

  1. Exception safety. If you throw, for example, a 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.
  2. Usability. Throwing derivitaves of 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.

like image 85
John Dibling Avatar answered Oct 19 '22 23:10

John Dibling


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.

like image 43
Nikos Athanasiou Avatar answered Oct 20 '22 00:10

Nikos Athanasiou