Exception recovery can be done in stages
try {...}
catch (someExn e) { // catches someExn or a subclass
// do part of recovery
throw otherExn{...};
}
- Throw some other exception
throw e; // throw the caught exception
throw; // throws original exception
All C++ library exceptions inherit from std::exception
. C++ does not require exceptions to inherit from std::exception
. In C++ you can throw anything.
Good Practice: Throw objects of existing exceptions or create your own exception classes
class BadInput {};
int n;
if (!(cin >> n)) throw BadInput {};
try {
foo();
} catch(BadInput &) {
n = 0;
}
Advice: catch by reference:
- out_of_range
- bad_alloc
- length_error
- Publish: Subject aggregates data
- Subscribe: Observer wants to be notified of new data
- Scroll IS-A Window
- Scroll HAS-A Window
Window *w = new BasicWindow();
w = new Scroll{w};
w = new Menu{Scroll};
w->display();