C++ Exception Handling - Try, Catch, and Throw


Exception handling is a crucial feature in C++ that allows you to handle unexpected errors and exceptional conditions gracefully. It helps prevent program crashes and ensures robust error handling. In this guide, we'll explore C++ exception handling using `try`, `catch`, and `throw`, and provide sample code to illustrate its usage.


Try, Catch, and Throw

In C++, you use the `try`, `catch`, and `throw` keywords to implement exception handling. The `try` block encloses the code that may raise an exception, the `catch` block handles the exception, and the `throw` statement is used to raise an exception explicitly. Here's a basic example:


#include <iostream>
using namespace std;
int divide(int dividend, int divisor) {
if (divisor == 0) {
throw "Division by zero is not allowed.";
}
return dividend / divisor;
}
int main() {
int dividend, divisor;
cout << "Enter dividend: ";
cin >> dividend;
cout << "Enter divisor: ";
cin >> divisor;
try {
int result = divide(dividend, divisor);
cout << "Result: " << result << endl;
} catch (const char* error) {
cout << "Exception caught: " << error << endl;
}
return 0;
}

In this example, the `divide` function may throw an exception when attempting to divide by zero. We use a `try` block to enclose the call to `divide`, and a `catch` block to handle the exception if it occurs. The `throw` statement is used to raise an exception with a message in the `divide` function.


Handling Multiple Exception Types

C++ allows you to handle different types of exceptions using multiple `catch` blocks. Each `catch` block can catch a specific exception type. Here's an example:


#include <iostream>
#include <stdexcept>
using namespace std;
int divide(int dividend, int divisor) {
if (divisor == 0) {
throw runtime_error("Division by zero is not allowed.");
}
return dividend / divisor;
}
int main() {
int dividend, divisor;
cout << "Enter dividend: ";
cin >> dividend;
cout << "Enter divisor: ";
cin >> divisor;
try {
int result = divide(dividend, divisor);
cout << "Result: " << result << endl;
} catch (const runtime_error& error) {
cout << "Runtime Error: " << error.what() << endl;
} catch (const exception& error) {
cout << "Exception caught: " << error.what() << endl;
}
return 0;
}

In this example, we use the standard C++ exception classes, and we catch `runtime_error` and `exception` types separately. This allows for more specific error handling based on the type of exception that occurs.


Custom Exception Classes

You can create custom exception classes by deriving from the `std::exception` class. Custom exceptions provide detailed information about the error. Here's an example:


#include <iostream>
#include <stdexcept>
using namespace std;
class CustomException : public exception {
public:
const char* what() const throw() {
return "Custom exception occurred.";
}
};
int divide(int dividend, int divisor) {
if (divisor == 0) {
throw CustomException();
}
return dividend / divisor;
}
int main() {
int dividend, divisor;
cout << "Enter dividend: ";
cin >> dividend;
cout << "Enter divisor: ";
cin >> divisor;
try {
int result = divide(dividend, divisor);
cout << "Result: " << result << endl;
} catch (const CustomException& error) {
cout << "Custom Exception: " << error.what() << endl;
} catch (const exception& error) {
cout << "Exception caught: " << error.what() << endl;
}
return 0;
}

In this example, we create a custom exception class `CustomException` that derives from `std::exception`. The `what` method is overridden to provide a custom error message. We then throw this custom exception in the `divide` function.


Conclusion

C++ exception handling using `try`, `catch`, and `throw` is a powerful mechanism for dealing with unexpected errors and exceptional conditions in your code. By defining your own exception classes and handling various types of exceptions, you can make your programs more robust and maintainable.