


Khusboo Tayal
Exception handling is a method of responding to runtime errors without crashing the program. It allows you to:
try {
// Code that may cause an exception
throw exception;
}
catch (exception_type identifier) {
// Code to handle the exception
}
#include <iostream>
using namespace std;
int main() {
try {
int a = 10, b = 0;
if (b == 0) {
throw "Division by zero error!";
}
cout << "Result: " << a / b << endl;
}
catch (const char* e) {
cout << "Exception caught: " << e << endl;
}
cout << "Program continues after exception." << endl;
return 0;
}Exception caught: Division by zero error! Program continues after exception.
#include <iostream>
using namespace std;
void checkNumber(int num) {
if (num < 0)
throw num; // Throwing an int
if (num == 0)
throw "Zero is not allowed."; // Throwing a const char*
}
int main() {
try {
checkNumber(-5);
}
catch (int e) {
cout << "Exception: Negative number - " << e << endl;
}
catch (const char* e) {
cout << "Exception: " << e << endl;
}
return 0;
}
Exception: Negative number - -5
You can use multiple catch blocks to handle different types of exceptions.
#include <iostream>
using namespace std;
int main() {
try {
int choice;
cout << "Enter 1 for int exception, 2 for double: ";
cin >> choice;
if (choice == 1)
throw 404;
else if (choice == 2)
throw 3.14;
else
throw "Invalid choice.";
}
catch (int e) {
cout << "Integer Exception Caught: " << e << endl;
}
catch (double e) {
cout << "Double Exception Caught: " << e << endl;
}
catch (...) { // Catches any other type
cout << "General Exception Caught." << endl;
}
return 0;
}
Enter 1 for int exception, 2 for double: 1 Integer Exception Caught: 404
Exceptions can also be thrown from functions and caught in the calling function.
#include <iostream>
using namespace std;
void divide(int a, int b) {
if (b == 0)
throw "Division by zero!";
cout << "Result: " << a / b << endl;
}
int main() {
try {
divide(10, 0);
}
catch (const char* e) {
cout << "Exception: " << e << endl;
}
return 0;
}
Exception: Division by zero!
You can nest try-catch blocks to handle exceptions at different levels.
#include <iostream>
using namespace std;
int main() {
try {
try {
throw "Inner exception!";
}
catch (const char* e) {
cout << "Caught in inner catch: " << e << endl;
throw; // Rethrowing exception
}
}
catch (const char* e) {
cout << "Caught in outer catch: " << e << endl;
}
return 0;
}
Caught in inner catch: Inner exception! Caught in outer catch: Inner exception!
You can create custom exception classes for better control.
#include <iostream>
#include <string>
using namespace std;
// Custom exception class
class InvalidAgeException {
private:
string message;
public:
InvalidAgeException(string msg) : message(msg) {}
string getMessage() const {
return message;
}
};
// Function using custom exception
void checkAge(int age) {
if (age < 0)
throw InvalidAgeException("Age cannot be negative.");
cout << "Your age is: " << age << endl;
}
int main() {
try {
checkAge(-5);
}
catch (InvalidAgeException &e) {
cout << "Exception: " << e.getMessage() << endl;
}
return 0;
}
Exception: Age cannot be negative.
C++ provides a set of standard exception classes in the <exception> header:
#include <iostream>
#include <exception>
using namespace std;
int main() {
try {
int *arr = new int[100000000000]; // Large memory allocation
}
catch (bad_alloc &e) {
cout << "Memory Allocation Error: " << e.what() << endl;
}
return 0;
}
Memory Allocation Error: std::bad_alloc
In this tutorial, you learned: