0

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?

3
  • 5
    An exception class doesn't throw exceptions. You throw an instance of an exception class. It holds data about the exception that you can retrieve later. They also don't handle exceptions. You catch those, too. Commented Aug 12, 2014 at 19:10
  • It's an exception, an anomaly, an error, an unexpected thing. It doesn't do anything, it just is. It's a class because classes are what we use to store information, and the details about the exception must be stored somewhere. Commented Aug 12, 2014 at 19:14
  • Exception classes are not the only throwable things, everything can be thrown (like an int, float, char *, std::string, ...). Commented Aug 12, 2014 at 19:16

1 Answer 1

3

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.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.