Summary of "6 04 Exception classes"
Summary of “6 04 Exception classes”
This video lecture, presented by the instructional staff of ECE 150 (Professors Patel, Verner Dietl, and Douglas Harder), covers the use of exception classes in C++, focusing on inheritance and polymorphism within the exception handling framework.
Main Ideas and Concepts
1. Recap of Inheritance and Polymorphism
- Inheritance allows extending base classes by modifying or adding functionality and member variables in derived classes.
- Polymorphism enables calling overridden functions of derived classes through base class references or pointers.
2. Exception Class Hierarchy in C++
- The base class for exceptions is the standard exception class (
std::exception), which is abstract and cannot be instantiated directly. - Two primary derived classes:
- Logic error (
std::logic_error) - Runtime error (
std::runtime_error)
- Logic error (
- Further derived classes from logic error:
std::domain_errorstd::invalid_argumentstd::length_errorstd::out_of_range
- Derived classes from runtime error:
std::range_errorstd::overflow_errorstd::underflow_error
3. Distinction Between Logic Errors and Runtime Errors
- Logic errors: Programmer errors that could have been prevented by code checks (e.g., invalid arguments, out-of-bounds access).
- Runtime errors: Errors only detectable during execution (e.g., overflow, underflow, system state issues).
4. Structure of Exception Classes
- Most exception classes store a string message and provide a virtual member function
what()that returns the error message as a C-style string. - The
what()function is markednoexcept, guaranteeing it does not throw exceptions.
5. Creating a Custom Exception Class with Inheritance
- Example:
int_domain_error, derived fromstd::domain_error. - Adds an integer member variable to store the problematic value causing the exception.
- Overrides the
what()function to:- Log a message indicating the function was called.
- Append the integer value to the error message.
- Adds a
value()function to retrieve the stored integer.
6. Using the Custom Exception Class
- Example function: recursive calculation of binomial coefficients.
- Throws
int_domain_errorwhen arguments are out of valid range, passing error messages and invalid values. - Demonstrates catching exceptions by base class references (e.g., catching
std::domain_errorinstead ofint_domain_error) and how polymorphism ensures the overriddenwhat()function in the derived class is called. - Shows limitations: base class references cannot access derived class-specific functions (e.g.,
value()), because the base class does not define them.
7. Polymorphism Illustrated with a Class Hierarchy Example
- Four classes: A (base), B (derived from A), C (derived from B), D (derived from C).
- Each class has member functions, some overridden in derived classes.
- Demonstrates calling member functions through references or pointers to base classes and how the most derived override is executed at runtime.
- Shows that pointers or references to base classes can refer to derived class instances, and virtual functions ensure the correct overridden version is called.
8. Summary of Polymorphism Features in Exception Handling
- Derived exception instances can be caught by base class catch blocks.
- Virtual functions (
what()) behave polymorphically, calling the most derived override. - Base class references or pointers can refer to derived objects, enabling flexible exception handling.
Methodology / Instructions Presented
Creating a Derived Exception Class
- Derive from an existing exception class (e.g.,
std::domain_error). - Add additional member variables (e.g., an integer to store error-related data).
- Override the
what()function to extend the error message and add logging. - Provide additional member functions to access new data members.
Throwing and Catching Exceptions
- Throw instances of the derived exception when an error condition occurs.
- Catch exceptions using either the derived class type or any of its base classes.
- Use polymorphism to ensure the correct
what()function is called even when caught as a base class.
Using Polymorphism with References and Pointers
- Assign derived class instances to base class references or pointers.
- Call virtual functions to ensure the derived class implementation is executed.
- Understand that base class references cannot access derived-only members.
Speakers / Sources Featured
- Professors Patel
- Verner Dietl
- Douglas Harder
(Instructional staff for ECE 150)
Category
Educational