According to this site it is perfectly usable to throw a string or integer. I find this to be pretty clean and easy to understand. What are the downsides to throw "description of what happened" rather than throw std::runtime_error("description of what happened")?
According to this site it is perfectly usable to throw a string or integer.
Share
That site is stupid, and teaches bad design.
If you throw
intorchar*, then you will have to catch it usingintorchar*only. You may qualify it withconst.If you throw
std::runtime_error, then you can catch it usingstd::runtime_error const &, or its base classstd::exception const &.So what is good about it?
The good about it is that if you throw an exception using a class which is ultimately derived from
std::exception, then you can write just ONEcatchblock which accepts the exception asstd::exception const&, irrespective of which derived class is used to throw exception.Here is one example:
Now the interesting part:
The good is that you are not required to write three
catchblocks just becausef()could throw three different types of exception. You may write more than onecatchto handle them differently if that benefits you in some way. But the point to be noted here is: that is not a requirement!In short, you can take advantage of the class hierarchy.