C++ ErrorsLesson 45Free preview
C++ Exceptions
This lesson is part of C++ · All levels
Summary
Exception handling separates the code that can fail from the code that handles the failure, using the try block to try, throw to throw the problem, and catch to handle it. With this mechanism, the program can recover from emergencies such as zero partitioning or memory depletion, instead of suddenly stopping. C++ also provides built-in exception classes such as invalid_argument, out_of_range, and bad_alloc of the header <stdexcept>.
Key points
- The throw statement is in the try, then passed to the appropriate catch.
- Catches are written based on type, from specific to general (exclusion).
- We can write our own exceptions by inheriting the std::exception.
- The catch block is ideally restored or gives a message that the user can understand.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.