throw non-exception objects
c++, exception
Solution
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`:
- 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`.
- 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`.
Problem
`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?