๐ŸŽ“ Unit 11: Exception Handling

Exception handling in C++ is a mechanism to detect and handle runtime errors gracefully, without crashing the program.


โœ๏ธ 1. Introduction to Exceptions

An exception is an unexpected situation or error that occurs during the execution of a program (e.g., divide by zero, invalid input, out-of-range access).

Exception handling mechanism provide a way to transfer control from one part of a program to another. This makes it easy to separate the error handling code from the code written to handle the actual functionality of the program.

C++ exception handling is built upon three keywords:

image.png


Syntax:

try {         
    throw val
} 
catch (ExceptionType e) {   
    // exception handling code
}

There are three types of values that can be thrown as an exception:

๐Ÿ”ธ Throwing Built-in Types