0
0
C++programming~20 mins

Multiple catch blocks in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Multiple Catch Blocks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of multiple catch blocks with integer exception
What is the output of this C++ code when an integer exception is thrown?
C++
#include <iostream>
using namespace std;

int main() {
    try {
        throw 10;
    } catch (double e) {
        cout << "Caught double: " << e << endl;
    } catch (int e) {
        cout << "Caught int: " << e << endl;
    } catch (...) {
        cout << "Caught unknown exception" << endl;
    }
    return 0;
}
ACaught double: 10
BCaught unknown exception
CCaught int: 10
DCompilation error
Attempts:
2 left
πŸ’‘ Hint
Remember that catch blocks are checked in order and the first matching type handles the exception.
❓ Predict Output
intermediate
2:00remaining
Output when throwing a string literal with multiple catch blocks
What will this C++ program output when it throws a string literal exception?
C++
#include <iostream>
using namespace std;

int main() {
    try {
        throw "error";
    } catch (int e) {
        cout << "Caught int: " << e << endl;
    } catch (const char* e) {
        cout << "Caught const char*: " << e << endl;
    } catch (...) {
        cout << "Caught unknown exception" << endl;
    }
    return 0;
}
ACaught const char*: error
BCaught unknown exception
CCaught int: error
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
Check which catch block matches the thrown type exactly.
πŸ”§ Debug
advanced
2:00remaining
Identify the error in multiple catch blocks
What error will this C++ code produce when compiled?
C++
#include <iostream>
using namespace std;

int main() {
    try {
        throw 5.5;
    } catch (int e) {
        cout << "Caught int: " << e << endl;
    } catch (double e) {
        cout << "Caught double: " << e << endl;
    } catch (double e) {
        cout << "Caught another double: " << e << endl;
    }
    return 0;
}
AOutput: Caught double: 5.5
BRuntime error: ambiguous catch block
COutput: Caught int: 5
DCompilation error: duplicate catch block for double
Attempts:
2 left
πŸ’‘ Hint
Check if catch blocks have unique exception types.
🧠 Conceptual
advanced
2:00remaining
Behavior of catch(...) block in multiple catch blocks
Consider the following C++ code snippet. What will be the output if an exception of type std::string is thrown?
C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    try {
        throw string("error");
    } catch (int e) {
        cout << "Caught int: " << e << endl;
    } catch (...) {
        cout << "Caught unknown exception" << endl;
    }
    return 0;
}
ACaught unknown exception
BProgram terminates without output
CCompilation error due to missing catch block for std::string
DCaught int: error
Attempts:
2 left
πŸ’‘ Hint
The catch(...) block catches any exception not caught by previous catch blocks.
❓ Predict Output
expert
3:00remaining
Output of nested try-catch with multiple catch blocks
What is the output of this C++ program with nested try-catch blocks and multiple catch handlers?
C++
#include <iostream>
using namespace std;

int main() {
    try {
        try {
            throw 'a';
        } catch (int e) {
            cout << "Inner caught int: " << e << endl;
        } catch (...) {
            cout << "Inner caught unknown" << endl;
            throw;
        }
    } catch (char e) {
        cout << "Outer caught char: " << e << endl;
    } catch (...) {
        cout << "Outer caught unknown" << endl;
    }
    return 0;
}
A
Inner caught int: a
Outer caught char: a
B
Inner caught unknown
Outer caught char: a
C
Inner caught unknown
Outer caught unknown
DCompilation error due to throw in catch block
Attempts:
2 left
πŸ’‘ Hint
Look at how the inner catch rethrows the exception and how the outer catch handles it.