Reputation: 57
So I know that an exception class can inherit from the std exception library, but what exactly does an exception class do? It throws exceptions and handles them, but why should I use a class? Can an exception class handle more than 1 type of object?
Upvotes: 0
Views: 178
Reputation: 153919
There is no such thing as an "exception class" in C++; there are
no constraints with regards to the type of what you can throw
and catch. (throw 3.14159;
is a perfectly legal C++
statement.) Good programming practice says that except in
special cases, you should throw objects which inherit from
std::exception
, but this is not a requirement, and it's not
unusual for programs to throw an int
to trigger the end of
program. (Calling exit
doesn't invoke all of the destructors,
so the program throws an int
, which is caught and returned in
main
.)
Upvotes: 3