Explain the concept of exception handling in C++ with suitable example.


Q.) Explain the concept of exception handling in C++ with suitable example.

Subject: object oriented programming

Exception Handling in C++:

Exception handling in C++ is a mechanism for detecting and responding to runtime errors and unexpected conditions that may occur during the execution of a program. It provides a way to handle these errors gracefully, allowing the program to continue execution instead of terminating abnormally.

Concept of Exception Handling:

  • Exception: An exception is an object that represents an unusual and abnormal condition that occurs during program execution. It can be thrown by code to indicate that an error has occurred.
  • Throwing an Exception: When an error or exceptional condition occurs, a C++ program can throw an exception using the throw keyword. The throw statement passes an exception object to the runtime system, which then initiates the exception-handling process.
  • Catching an Exception: To handle an exception, C++ provides the try-catch block. The try block contains the code that might throw an exception, and the catch block contains the code that will handle the exception if it is thrown.
  • Exception Propagation: If an exception is not handled within the current try-catch block, it is propagated to the caller of the current function. This process continues up the call stack until the exception is either handled by a catch block or reaches the main function, in which case the program terminates abnormally.

Example:

Consider the following C++ program that demonstrates exception handling:

#include 

using namespace std;

void divide(int a, int b)
{
    if (b == 0)
    {
        throw runtime_error("Division by zero");
    }
    cout << "Result: " << a / b << endl;
}

int main()
{
    try
    {
        divide(10, 2);
        divide(10, 0); // This line will throw an exception
    }
    catch (runtime_error& e)
    {
        cout << "Error: " << e.what() << endl;
    }
    catch (...) // Catch all other exceptions
    {
        cout << "Unknown error occurred" << endl;
    }

    return 0;
}

In this example, the divide() function takes two integer arguments, a and b, and divides them. If b is zero, the function throws a runtime_error exception with the message "Division by zero".

In the main() function, the try block contains the code that might throw an exception, which is the call to divide(10, 2) and divide(10, 0). The catch block contains the code that will handle the exception if it is thrown.

When divide(10, 2) is called, it executes successfully and prints the result. However, when divide(10, 0) is called, it throws a runtime_error exception with the message "Division by zero". This exception is caught by the catch block, which prints the error message.

The catch (...) block is a catch-all block that handles any exception that is not caught by the previous catch blocks. In this example, it would catch any other unexpected exceptions that may occur in the try block.

Advantages of Exception Handling:

  • Improves program robustness by providing a way to handle runtime errors gracefully, allowing the program to continue execution instead of terminating abnormally.
  • Simplifies error handling by centralizing error-handling code in one place, making it easier to debug and maintain.
  • Promotes code reusability by allowing error-handling code to be reused in multiple parts of the program.

Conclusion:

Exception handling is a vital aspect of C++ programming that allows developers to write robust and reliable programs. By using exception handling techniques, programmers can gracefully handle runtime errors and unexpected conditions, ensuring that their programs can continue execution even in the face of errors.