C++ (Module 5)
C++ (Module 5)
HANDLING IN C++
MODULE 5
CONTENTS
Exception Handling:
• Introduction to Exception
2
EXCEPTION
• Types of Exception:
1. Synchronous Exceptions
2. Asynchronous Exceptions
3
EXCEPTION
1. Synchronous Exception:
• Exception Occurred due to logical errors or invalid operations during program
execution.
• Examples:
• Division by zero
• Array index out of bounds
• File not found
• In C++ exception handling mechanism is designed to handle only synchronous
exceptions.
4
EXCEPTION
2. Asynchronous Exceptions:
• Caused by external events unrelated to program flow.
• Examples:
• Hardware failure
• Power Failure
• User interruption
• Segmentation fault
• Out of Memory
• Asynchronous Exceptions cannot be handled by C++ exception handling mechanism.
5
EXCEPTION HANDLING
6
EXCEPTION HANDLING
• try
• throw
• catch
7
EXCEPTION HANDLING
try block – block of statements which detects exception and throws the
exception (if detected).
• Syntax:
try
{
……………
throws exception;
…………….
…………….
}
8
EXCEPTION HANDLING
throw statement – When an exception is detected in the try block, then the
exception is thrown using the throw keyword
• Syntax:
try
{
……………
throws exception;
…………….
…………….
}
9
EXCEPTION HANDLING
cout << "Enter the value of a: "; throw "Division by zero error!";
}
cin >> a;
}
cout << "Enter the value of b: "; catch (char* msg) {
cin >> b; // This catches exceptions thrown as a char* (string
cout << "Enter the value of c: "; exceptions), such as: throw “Division by zero error!“.
cout << "Error: " << msg << endl;
cin >> c;
}
return 0;
} 11
EXCEPTION HANDLING
• char* msg is used to catch raw string errors thrown like throw "error".
• Use std::exception and its subclasses in modern C++ that are pre-defined in the headerfile
<stdexcept>
12
EXCEPTION HANDLING
13
EXCEPTION HANDLING
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int index;
cout << "Enter an index to access the array: ";
cin >> index;
try
{
int value = getValueAtIndex(arr, index, size);
cout << "Value at index " << index << " is: " << value << endl;
}
catch (const out_of_range& e)
{
cout << "Exception caught: " << e.what() << endl;
}
return 0; 14
}
EXCEPTION HANDLING
Explanation:
• out_of_range: A standard C++ exception class, derived from std::logic_error, used when you
try to access an element outside the valid range (e.g., accessing a vector
element at an invalid index).
• out_of_range& e: Catch the exception by const reference. This avoids copying the exception
object that is “e” and preserves polymorphism.
• e.what(): Returns a const char* message describing the exception. This is part of the
std::exception interface.
15
EXCEPTION HANDLING
Multiple catch blocks: Multiple catch blocks allow a program to handle different types of exceptions
thrown from a single try block. Each catch block is designed to handle a specific type of exception.
Syntax:
try {
// Code that may throw different types of exceptions
}
catch (int e) {
// Handle integer exceptions
}
catch (const char* msg) {
// Handle string exceptions
}
catch (...) {
// Handle any other type of exception (generic catch)
}
16
EXCEPTION HANDLING
Example:
Q. Write a C++ program to compute the roots of a quadratic equation of
the form ax² + bx + c = 0.
• If the roots are real and unequal, display them.
• If the roots are equal, throw and handle a logic_error exception with the message "Roots are
equal".
• If the roots are imaginary, throw and handle a runtime_error exception with the message
"Imaginary roots - cannot compute".
• Use appropriate try, catch, and throw statements to handle exceptions.
17
EXCEPTION HANDLING
PROGRAM:
#include <iostream>
#include <cmath>
#include <stdexcept>
using namespace std;
int main() {
double a, b, c;
cout << "Enter coefficients a, b, and c: ";
cin >> a >> b >> c;
try {
findRoots(a, b, c);
}
catch (const logic_error &e) {
cout << "Exception: " << e.what() << endl;
}
catch (const runtime_error &e) {
cout << "Exception: " << e.what() << endl;
}
return 0;
} 19
BENEFITS OF EXCEPTION HANDLING
21
PREDEFINED EXCEPTION
Predefined exceptions in C++ are part of the Standard Library (specifically from the <stdexcept>
header). They are ready-to-use exception classes derived from the base class std::exception.
Types:
22