0
0
C++programming~20 mins

Exception handling flow in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Exception Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested try-catch blocks
What is the output of this C++ code when executed?
C++
#include <iostream>

int main() {
    try {
        try {
            throw 10;
        } catch (int e) {
            std::cout << "Inner catch: " << e << std::endl;
            throw;
        }
    } catch (int e) {
        std::cout << "Outer catch: " << e << std::endl;
    }
    return 0;
}
ANo output, program terminates silently
B
Inner catch: 10
Outer catch: 10
COuter catch: 10
DInner catch: 10
Attempts:
2 left
πŸ’‘ Hint
Remember that rethrowing an exception passes it to the next matching catch block outside.
❓ Predict Output
intermediate
2:00remaining
Exception caught by base class catch block
What will be the output of this C++ program?
C++
#include <iostream>
#include <exception>

class MyException : public std::exception {};

int main() {
    try {
        throw MyException();
    } catch (std::exception& e) {
        std::cout << "Caught std::exception" << std::endl;
    } catch (...) {
        std::cout << "Caught unknown exception" << std::endl;
    }
    return 0;
}
ACaught std::exception
BNo output
CCaught unknown exception
DCompilation error
Attempts:
2 left
πŸ’‘ Hint
MyException inherits from std::exception, so it matches the first catch.
❓ Predict Output
advanced
2:00remaining
Exception propagation with function calls
What is the output when this C++ program runs?
C++
#include <iostream>

void funcB() {
    throw "Error in funcB";
}

void funcA() {
    try {
        funcB();
    } catch (const char* msg) {
        std::cout << "Caught in funcA: " << msg << std::endl;
        throw;
    }
}

int main() {
    try {
        funcA();
    } catch (const char* msg) {
        std::cout << "Caught in main: " << msg << std::endl;
    }
    return 0;
}
ACaught in funcA: Error in funcB
BCaught in main: Error in funcB
C
Caught in funcA: Error in funcB
Caught in main: Error in funcB
DNo output, program crashes
Attempts:
2 left
πŸ’‘ Hint
Exception thrown in funcB is caught and rethrown in funcA, then caught in main.
❓ Predict Output
advanced
2:00remaining
Exception handling with multiple catch blocks
What output does this program produce?
C++
#include <iostream>

int main() {
    try {
        throw 3.14;
    } catch (int e) {
        std::cout << "Caught int: " << e << std::endl;
    } catch (double e) {
        std::cout << "Caught double: " << e << std::endl;
    } catch (...) {
        std::cout << "Caught unknown" << std::endl;
    }
    return 0;
}
ACaught double: 3.14
BCaught int: 3
CCaught unknown
DNo output
Attempts:
2 left
πŸ’‘ Hint
The thrown value is a double, so the matching catch block handles it.
🧠 Conceptual
expert
2:00remaining
Exception handling order and program termination
Consider this C++ code snippet. What happens when an exception is thrown but not caught anywhere?
C++
#include <iostream>
#include <stdexcept>

void func() {
    throw std::runtime_error("Error");
}

int main() {
    func();
    std::cout << "End of main" << std::endl;
    return 0;
}
AProgram prints "End of main" and then throws exception
B"End of main" is printed, then program terminates normally
CProgram compiles but does nothing
DProgram terminates with uncaught exception error, "End of main" is not printed
Attempts:
2 left
πŸ’‘ Hint
If no catch block handles the exception, the program ends abruptly.