Bird
Raised Fist0
C++programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main purpose of using multiple catch blocks in C++?

easy
A. To speed up the program execution
B. To handle different types of exceptions separately
C. To avoid using try blocks
D. To declare multiple variables at once

Solution

  1. Step 1: Understand the role of catch blocks

    Catch blocks are used to handle exceptions that occur in the try block.
  2. Step 2: Recognize the benefit of multiple catch blocks

    Multiple catch blocks allow handling different exception types separately, so each error can be managed properly.
  3. Final Answer:

    To handle different types of exceptions separately -> Option B
  4. Quick Check:

    Multiple catch blocks = handle different exceptions [OK]
Hint: Multiple catch blocks handle different errors separately [OK]
Common Mistakes:
  • Thinking multiple catch blocks speed up code
  • Confusing catch blocks with variable declarations
  • Believing try blocks are not needed with multiple catch blocks
2.

Which of the following is the correct syntax to catch any remaining exceptions in multiple catch blocks in C++?

try {
    // code
}
catch (int e) {
    // handle int
}
_____ {
    // handle other
}
easy
A. catch (...)
B. catch (string e)
C. catch (double e)
D. catch (char e)

Solution

  1. Step 1: Identify the syntax for catch-all block

    The catch(...) block catches any exception type not caught by previous catch blocks.
  2. Step 2: Confirm correct syntax

    Only catch(...) uses ellipsis and no variable type, making it the correct syntax for a generic catch block.
  3. Final Answer:

    catch (...) -> Option A
  4. Quick Check:

    catch(...) = catch all exceptions [OK]
Hint: Use catch(...) to catch all exceptions generically [OK]
Common Mistakes:
  • Using catch without parentheses
  • Trying to catch unknown types without ellipsis
  • Misplacing catch blocks after try
3.

What will be the output of the following C++ code?

try {
    throw 10;
}
catch (double e) {
    std::cout << "Double exception";
}
catch (int e) {
    std::cout << "Integer exception: " << e;
}
medium
A. Compilation error
B. Double exception
C. No output
D. Integer exception: 10

Solution

  1. Step 1: Identify the thrown exception type

    The code throws an integer exception with value 10.
  2. Step 2: Match thrown exception with catch blocks

    The first catch block expects a double, so it is skipped. The second catch block matches int, so it executes and prints the message.
  3. Final Answer:

    Integer exception: 10 -> Option D
  4. Quick Check:

    throw int = catch int block runs [OK]
Hint: Match thrown type with catch parameter type [OK]
Common Mistakes:
  • Assuming first catch always runs
  • Confusing int and double types
  • Expecting no output if types differ
4.

Find the error in this code snippet using multiple catch blocks:

try {
    throw "error";
}
catch (int e) {
    std::cout << "Int error";
}
catch (char e) {
    std::cout << "Char error";
}
medium
A. Missing catch block for const char* exception
B. Try block syntax is incorrect
C. Cannot throw string literals
D. Catch blocks must be in reverse order

Solution

  1. Step 1: Identify the thrown exception type

    The code throws a string literal, which is of type const char*.
  2. Step 2: Check catch blocks for matching types

    Catch blocks handle int and char, but not const char*. So no catch block matches the thrown exception, causing an unhandled exception.
  3. Final Answer:

    Missing catch block for const char* exception -> Option A
  4. Quick Check:

    Thrown type must match catch parameter [OK]
Hint: Catch blocks must match thrown exception types exactly [OK]
Common Mistakes:
  • Assuming char catch handles string literals
  • Thinking try block syntax is wrong
  • Believing catch order matters for unrelated types
5.

Consider this code with multiple catch blocks:

try {
    throw std::runtime_error("runtime error");
}
catch (std::exception& e) {
    std::cout << "Exception: " << e.what();
}
catch (std::runtime_error& e) {
    std::cout << "Runtime error: " << e.what();
}

What will be the output and why?

hard
A. Exception: runtime error
B. Runtime error: runtime error
C. Compilation error due to catch order
D. No output because exception is not caught

Solution

  1. Step 1: Understand exception inheritance

    std::runtime_error is derived from std::exception, so it matches both catch blocks.
  2. Step 2: Analyze catch block order

    The first catch block catches std::exception by reference, so it catches the thrown runtime_error before the second catch block is reached.
  3. Step 3: Determine output

    The first catch block prints "Exception: runtime error" using e.what().
  4. Final Answer:

    Compilation error due to catch order -> Option C
  5. Quick Check:

    Derived class catch must come before base class catch to avoid compilation error [OK]
Hint: Catch derived exceptions before base to avoid compilation error [OK]
Common Mistakes:
  • Expecting derived catch to run first
  • Thinking catch order causes no compilation error
  • Assuming no output if multiple matches
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard