WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C++ · Basic · question 20 of 100

How would you handle exception handling in C++? Can you provide an example using try-catch blocks?

📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

Exception handling is a mechanism in C++ that allows errors and exceptional conditions to be handled in a controlled and structured manner. Exception handling is important because it allows programs to recover from errors and continue executing, rather than crashing or exiting unexpectedly.

In C++, exception handling is done using try-catch blocks. The ’try’ block contains the code that may throw an exception, and the ’catch’ block handles the exception if it is thrown. The ’catch’ block specifies the type of exception to be caught, and can also provide additional information about the exception.

Here is an example of using try-catch blocks for exception handling:

    #include <iostream>
    
    int main() {
        try {
            int x = 10;
            int y = 0;
            if (y == 0) {
                throw std::runtime_error("Division by zero");
            }
            int result = x / y;
            std::cout << "Result: " << result << std::endl;
        }
        catch (std::exception& e) {
            std::cout << "Exception caught: " << e.what() << std::endl;
        }
        return 0;
    }

In this example, we are attempting to divide the integer variable ’x’ by the integer variable ’y’. However, if ’y’ is equal to zero, we throw a runtime_error exception with the message "Division by zero". The try-catch block catches the exception and prints the message to the console using the ’what()’ function of the exception object.

Another way to catch exceptions is to use multiple catch blocks to handle different types of exceptions. For example:

    #include <iostream>
    
    int main() {
        try {
            // code that may throw an exception
        }
        catch (std::runtime_error& e) {
            // handle runtime_error exception
        }
        catch (std::logic_error& e) {
            // handle logic_error exception
        }
        catch (...) {
            // handle all other exceptions
        }
        return 0;
    }

In this example, we have multiple catch blocks that handle different types of exceptions. The first catch block catches exceptions of type std::runtime_error, the second catch block catches exceptions of type std::logic_error, and the third catch block catches all other types of exceptions using the ellipsis (...) syntax.

In summary, exception handling is an important mechanism in C++ that allows errors and exceptional conditions to be handled in a controlled and structured manner. Exception handling is done using try-catch blocks, which allow programs to recover from errors and continue executing.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C++ interview — then scores it.
📞 Practice C++ — free 15 min
📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

All 100 C++ questions · All topics