Exception Handling Programs
C++ exception handling programs: try, catch, and throw, multiple catch blocks, custom exception classes, noexcept, nested try-catch, and rethrowing errors.
5 programs · open one, then the next
Start with Basic try-catch- 01Basic try-catchBeginnerLearn try, catch, and throw in C++ with a division-by-zero example: how throw leaves a function, how catch matches the type, and how the program keeps running.Open example →cpp#include <iostream> using namespace std; int divide(int a, int b) {
- 02Multiple catch BlocksIntermediateHandle several exception types in C++ with multiple catch blocks: invalid_argument, out_of_range, runtime_error, and catch(...), and why order matters.Open example →cpp#include <iostream> #include <stdexcept> using namespace std;
- 03Custom Exception ClassIntermediateCreate a custom exception class in C++ by deriving from std::exception and overriding what(). Includes an AgeException that carries the bad value to the handler.Open example →cpp#include <iostream> #include <exception> #include <string> using namespace std;
- 04Exception SpecificationIntermediateLearn noexcept in C++: mark functions that never throw, test expressions with the noexcept operator, and see why throw(type) specifications were removed in C++17.Open example →cpp#include <iostream> #include <stdexcept> using namespace std;
- 05Nested try-catchIntermediateNested try-catch in C++: an inner handler catches one type and rethrows with throw;, the outer handler catches the rest, and why the base class matters there.Open example →cpp#include <iostream> #include <stdexcept> using namespace std;