Lab Assignment 6 (Exception Handling)
Lab Assignment 6 (Exception Handling)
Exception handling in C++ is a mechanism that allows a program to deal with unexpected situations or
errors during runtime, without terminating abruptly. This feature provides a robust way to ensure
program stability and is implemented using three primary keywords: try, catch, and throw.
1. Throwing an Exception:
When an error or unexpected situation occurs, the program "throws" an exception using the throw
keyword.
Example:
if (denominator == 0) {
throw "Division by zero!";
}
2. Catching an Exception:
After an exception is thrown, the program transfers control to a catch block that matches the type of the
exception.
Example:
catch (const char* msg) {
cout << msg << endl;
}
3. Try Block: