Bird
Raised Fist0
C++programming~20 mins

Try–catch block in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Try–catch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of try–catch with integer exception
What is the output of this C++ code?
C++
#include <iostream>

int main() {
    try {
        throw 5;
    } catch (int e) {
        std::cout << "Caught integer: " << e << std::endl;
    }
    return 0;
}
ACaught integer: 0
BCaught integer: 5
CNo output
DCompilation error
Attempts:
2 left
💡 Hint
The throw statement throws an integer, which is caught by the catch block.
❓ Predict Output
intermediate
2:00remaining
Output when no exception is thrown
What will this C++ program output?
C++
#include <iostream>

int main() {
    try {
        std::cout << "Inside try block" << std::endl;
    } catch (...) {
        std::cout << "Caught exception" << std::endl;
    }
    std::cout << "After try-catch" << std::endl;
    return 0;
}
A
Inside try block
After try-catch
BCompilation error
CInside try block
D
Caught exception
After try-catch
Attempts:
2 left
💡 Hint
No exception is thrown, so catch block is skipped.
❓ Predict Output
advanced
2:00remaining
Output with multiple catch blocks
What is the output of this C++ code?
C++
#include <iostream>

int main() {
    try {
        throw 'a';
    } catch (int e) {
        std::cout << "Caught int: " << e << std::endl;
    } catch (char e) {
        std::cout << "Caught char: " << e << std::endl;
    } catch (...) {
        std::cout << "Caught unknown exception" << std::endl;
    }
    return 0;
}
ACompilation error
BCaught int: 97
CCaught unknown exception
DCaught char: a
Attempts:
2 left
💡 Hint
The thrown type is char, so the matching catch block runs.
❓ Predict Output
advanced
2:00remaining
Exception not caught by specific catch block
What happens when this C++ code runs?
C++
#include <iostream>

int main() {
    try {
        throw 3.14;
    } catch (int e) {
        std::cout << "Caught int: " << e << std::endl;
    }
    return 0;
}
AProgram terminates with uncaught exception
BCaught int: 3
CCaught int: 3.14
DCompilation error
Attempts:
2 left
💡 Hint
The thrown type is double but catch expects int.
🧠 Conceptual
expert
3:00remaining
Behavior of nested try–catch blocks
Consider this C++ code with nested try–catch blocks. What is the output?
C++
#include <iostream>

int main() {
    try {
        try {
            throw 10;
        } catch (double e) {
            std::cout << "Inner catch double" << std::endl;
        }
    } catch (int e) {
        std::cout << "Outer catch int: " << e << std::endl;
    }
    return 0;
}
ANo output
BInner catch double
COuter catch int: 10
DCompilation error
Attempts:
2 left
💡 Hint
The inner catch does not catch int, so exception propagates to outer catch.

Practice

(1/5)
1.

What is the main purpose of a try-catch block in C++?

easy
A. To handle errors and exceptions safely during program execution
B. To speed up the program by skipping code
C. To declare variables globally
D. To create loops that run multiple times

Solution

  1. Step 1: Understand the role of try block

    The try block contains code that might cause an error or exception.
  2. Step 2: Understand the role of catch block

    The catch block handles the error so the program does not crash.
  3. Final Answer:

    To handle errors and exceptions safely during program execution -> Option A
  4. Quick Check:

    Try-catch handles errors = B [OK]
Hint: Try-catch blocks catch errors to keep programs running [OK]
Common Mistakes:
  • Thinking try-catch speeds up code
  • Confusing try-catch with loops
  • Using try-catch to declare variables
2.

Which of the following is the correct syntax to start a try block in C++?

?
easy
A. catch {
B. try (
C. try:
D. try {

Solution

  1. Step 1: Recall try block syntax

    The try block starts with the keyword try followed by curly braces { }.
  2. Step 2: Identify correct option

    Only try { uses try { which is correct syntax to start the block.
  3. Final Answer:

    try { -> Option D
  4. Quick Check:

    Try block starts with try { = C [OK]
Hint: Try block always starts with 'try {' in C++ [OK]
Common Mistakes:
  • Using parentheses instead of curly braces
  • Starting with catch instead of try
  • Using colon after try
3.

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

try {
    int x = 5 / 0;
    std::cout << "Result: " << x << std::endl;
} catch (const std::exception& e) {
    std::cout << "Caught exception" << std::endl;
}
medium
A. Result: 0
B. Caught exception
C. Program crashes with division by zero error
D. No output

Solution

  1. Step 1: Analyze division by zero in C++

    Integer division by zero causes undefined behavior and usually crashes the program; it does not throw a standard exception.
  2. Step 2: Check catch block type

    The catch block catches std::exception types, but division by zero does not throw this exception automatically.
  3. Final Answer:

    Program crashes with division by zero error -> Option C
  4. Quick Check:

    Integer division by zero crashes = D [OK]
Hint: Division by zero crashes, not caught by std::exception [OK]
Common Mistakes:
  • Assuming catch block catches all errors
  • Expecting output 'Caught exception'
  • Thinking division by zero returns zero
4.

Identify the error in this C++ code snippet:

try {
    throw 10;
} catch (int e) {
    std::cout << "Caught int: " << e << std::endl;
} catch (...) {
    std::cout << "Caught unknown" << std::endl;
}
medium
A. The catch block for int should come after catch(...)
B. No error; code is correct
C. Multiple catch blocks are not allowed
D. Throwing int is invalid

Solution

  1. Step 1: Check throw and catch types

    The code throws an int and catches it with catch(int e), which matches correctly.
  2. Step 2: Verify catch block order

    The specific catch catch(int e) comes before the catch-all catch(...), which is correct.
  3. Final Answer:

    No error; code is correct -> Option B
  4. Quick Check:

    Throw int caught by int catch first = A [OK]
Hint: Specific catch blocks must come before catch(...) [OK]
Common Mistakes:
  • Placing catch(...) before specific catch
  • Thinking multiple catch blocks are invalid
  • Believing throwing int is not allowed
5.

Consider this code snippet:

try {
    std::string s = "hello";
    int n = std::stoi(s);
    std::cout << n << std::endl;
} catch (const std::invalid_argument& e) {
    std::cout << "Invalid argument caught" << std::endl;
} catch (...) {
    std::cout << "Other exception caught" << std::endl;
}

What will be the output and why?

hard
A. Invalid argument caught
B. Other exception caught
C. hello
D. Program crashes

Solution

  1. Step 1: Understand std::stoi behavior

    Trying to convert "hello" to int using std::stoi throws std::invalid_argument exception because "hello" is not a number.
  2. Step 2: Check catch blocks

    The first catch block catches std::invalid_argument exceptions and prints "Invalid argument caught".
  3. Final Answer:

    Invalid argument caught -> Option A
  4. Quick Check:

    std::stoi throws invalid_argument for non-numeric string = A [OK]
Hint: std::stoi throws invalid_argument on bad input [OK]
Common Mistakes:
  • Expecting program to print 'hello'
  • Assuming catch(...) runs instead of specific catch
  • Thinking program crashes without catch
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