Introduction to C++ | Exception Handling
Exception Handling in C++
In C++, exception handling provides a mechanism to respond to runtime errors in a controlled way. Using exceptions, you can manage errors without crashing the program, ensuring smooth execution.
What is Exception Handling?
Exception handling in C++ is done using three main keywords:
- try: A block of code that contains statements that might throw an exception.
- throw: Used to throw an exception when an error condition occurs.
- catch: Used to handle exceptions thrown by the
throw
statement.
Basic Syntax of Exception Handling
The basic syntax of exception handling involves a try
block followed by one or more catch
blocks:
Code Example: Basic Exception Handling
#include <iostream>
using namespace std;
int main() {
try {
// Code that may throw an exception
int x = -1;
if (x < 0) {
throw "Negative number error"; // Throwing an exception
}
}
catch (const char* msg) {
cout << "Exception: " << msg << endl; // Handling the exception
}
return 0;
}
Output
Multiple Catch Blocks
You can handle different types of exceptions by using multiple catch
blocks. This helps to catch specific types of exceptions and handle them accordingly.
Code Example: Multiple Catch Blocks
#include <iostream>
using namespace std;
int main() {
try {
int x = 0;
if (x == 0) {
throw 0; // Integer exception
}
else {
throw "Some error"; // String exception
}
}
catch (int e) {
cout << "Integer exception: Division by zero" << endl;
}
catch (const char* msg) {
cout << "String exception: " << msg << endl;
}
return 0;
}
Output
Exception Propagation
When an exception is thrown in a function, it propagates up the call stack until it is caught by a catch
block. If no catch
block is found, the program terminates.
Code Example: Exception Propagation
#include <iostream>
using namespace std;
void func() {
throw "Error occurred in func"; // Exception thrown in function
}
int main() {
try {
func(); // Calling the function that throws an exception
}
catch (const char* msg) {
cout << "Caught exception: " << msg << endl;
}
return 0;
}
Output
Throwing Different Data Types
In C++, you can throw any type of object (including primitive types, classes, and user-defined objects) as exceptions.
Code Example: Throwing Different Data Types
#include <iostream>
using namespace std;
int main() {
try {
throw 42; // Throwing an integer
}
catch (int e) {
cout << "Caught integer exception: " << e << endl;
}
try {
throw 3.14; // Throwing a float
}
catch (double e) {
cout << "Caught double exception: " << e << endl;
}
return 0;
}
Output
Caught double exception: 3.14
Standard Exceptions
C++ provides several standard exceptions as part of the stdexcept
header, such as:
- std::exception: Base class for all standard exceptions.
- std::runtime_error: For errors that occur during program execution.
- std::out_of_range: For invalid accesses to data structures like arrays or vectors.
- std::invalid_argument: For invalid arguments passed to functions.
Example: Standard Exception Handling
Code Example: Standard Exception Handling
#include <iostream>
#include <stdexcept>
using namespace std;
int main() {
try {
throw runtime_error("Runtime error occurred!"); // Throwing a standard exception
}
catch (const runtime_error& e) {
cout << "Caught exception: " << e.what() << endl;
}
return 0;
}
Output
Pro Tip:
π‘ Pro Tip
Itβs a good practice to catch exceptions by reference (e.g., catch (const exception& e)
) to avoid slicing and unnecessary copies of the exception objects.