This section provides an introduction to the fundamental concepts of exception handling in C++ with a focus on understanding and handling a common runtime error: divide-by-zero. Through this section, students will learn how exception handling provides a way to respond to exceptional circumstances (like divide-by-zero errors) in programs by transferring control to special functions called handlers.
An exception is an event that occurs during the execution of a program that disrupts the normal flow of the program's instructions. In C++ programming, exception handling is managed with three keywords: try
, catch
, and throw
.
try
: A block of code that might throw an exception. It's followed by one or more catch blocks.catch
: A block of code that handles an exception. It specifies the type of exception it can handle.throw
: Used to actually pass an exception. An exception can be any object.
Dividing by zero is a common error that occurs in programs when a division operation in the code attempts to divide a number by zero. This is undefined behavior in C++ and can cause programs to crash or produce invalid results. Exception handling allows us to manage this error gracefully.
#include <iostream>
#include <stdexcept> // Include for std::runtime_error
double safe_divide(double numerator, double denominator) {
if (denominator == 0) {
throw std::runtime_error("Math error: Attempted to divide by Zero\n");
}
return numerator / denominator;
}
int main() {
double numerator = 10.0;
double denominator = 0.0;
try {
double result = safe_divide(numerator, denominator);
std::cout << "The result is: " << result << std::endl;
} catch (const std::runtime_error& e) {
std::cerr << "Exception occurred: " << e.what() << std::endl;
}
return 0;
}
In the example above:
- We define a function
safe_divide
that takes twodouble
values as arguments. - Inside
safe_divide
, we check if thedenominator
is zero. If it is, wethrow
astd::runtime_error
with an appropriate error message. - In
main
, we callsafe_divide
inside atry
block which is followed by acatch
block designed to catchstd::runtime_error
exceptions. - If an exception is thrown, the
catch
block catches it and prints the error message.
By running this program, you'll see that it doesn't crash; instead, it prints the error message "Exception occurred: Math error: Attempted to divide by Zero\n".
- Always check for divide-by-zero and other error conditions before performing arithmetic operations.
- Use exceptions to signal the occurrence of an exceptional event that cannot be handled in the local scope.
- Write catch blocks to handle specific types of exceptions when possible.
- Assignment 1: Modify the above example to handle multiple exceptions.
- Assignment 2: Write a function that performs a series of arithmetic operations and includes proper exception handling.
This section provided an introduction to exception handling with a simple divide-by-zero example. Understanding and using exception handling effectively can lead to more robust and reliable C++ applications.