This complete tutorial on C++ Exception Handling explains how to handle runtime errors safely using try, catch, and throw. It also covers multiple catch blocks, user-defined exceptions, and the noexcept keyword. Following best practices, it helps learners write robust and fault-tolerant C++ programs.
Exception Handling – Complete Tutorial
1. What is Exception Handling?
Exception handling allows a program to respond to runtime errors gracefully, without crashing.
- Separates normal code from error handling code
- Provides control over unexpected situations
2. try, catch, throw
try– block of code where exceptions might occurthrow– used to signal an exceptioncatch– handles the thrown exception
Example:
#include <iostream>
using namespace std;
int main() {
try {
int x = 10, y = 0;
if(y == 0) throw "Division by zero!";
cout << x/y << endl;
}
catch(const char* e) {
cout << "Error: " << e << endl;
}
}
3. Multiple Catch Blocks
You can have multiple catch blocks to handle different exception types.
Example:
try {
int x = -1;
if(x < 0) throw x; // integer exception
else throw "Negative"; // string exception
}
catch(int e) {
cout << "Integer exception: " << e << endl;
}
catch(const char* e) {
cout << "String exception: " << e << endl;
}
Key Points:
- Catch blocks are checked in order
- Most specific exception type should come first
4. User-Defined Exceptions
You can create custom exception classes to provide meaningful error messages.
Example:
#include <iostream>
#include <exception>
using namespace std;
class MyException : public exception {
public:
const char* what() const noexcept override {
return "Custom exception occurred!";
}
};
int main() {
try {
throw MyException();
}
catch(const MyException &e) {
cout << e.what() << endl;
}
}
Notes:
- Derive from
std::exceptionfor compatibility - Override
what()for descriptive messages
5. noexcept
noexcept indicates that a function does not throw exceptions.
Example:
void safeFunc() noexcept {
cout << "This function does not throw" << endl;
}
int main() {
safeFunc();
}
Benefits:
- Helps compiler optimization
- Provides guarantee of exception safety
Best Practices
- Use exception handling for exceptional conditions, not normal logic
- Catch exceptions by reference to avoid slicing
- Always provide descriptive messages
- Prefer standard exceptions where possible (
std::runtime_error,std::out_of_range) - Use
noexceptfor functions that guarantee no exceptions
Common Mistakes
- Catching exceptions by value instead of reference
- Using exceptions for control flow
- Throwing built-in types instead of
std::exception - Ignoring memory/resource cleanup (use RAII to prevent leaks)
Summary
In this chapter, you learned about C++ Exception Handling, including:
try,catch, andthrowsyntax- Handling multiple exception types
- Creating user-defined exceptions
- Using
noexceptfor safe functions
Exception handling helps write robust, maintainable, and safe C++ programs by properly managing runtime errors.