Bird
Raised Fist0
C++programming~20 mins

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

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

Practice

(1/5)
1. What is the main purpose of using try and catch blocks in C++?
easy
A. To speed up the program execution
B. To handle errors and keep the program running smoothly
C. To declare variables
D. To create loops

Solution

  1. Step 1: Understand the role of try block

    The try block contains code that might cause errors or exceptions during execution.
  2. Step 2: Understand the role of catch block

    The catch block handles those errors so the program does not crash and can continue running.
  3. Final Answer:

    To handle errors and keep the program running smoothly -> Option B
  4. Quick Check:

    Exception handling = handle errors safely [OK]
Hint: Try-catch blocks catch errors to avoid crashes [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 catch an exception of type int in C++?
easy
A. catch (int e) { /* handle error */ }
B. catch int (e) { /* handle error */ }
C. catch { int e; /* handle error */ }
D. catch (e int) { /* handle error */ }

Solution

  1. Step 1: Recall correct catch syntax

    The catch block must specify the exception type in parentheses followed by a variable name, like catch (int e).
  2. Step 2: Identify syntax errors in other options

    The other options contain syntax errors such as reversed type and variable order, incorrect keyword placement, or missing parentheses with improper declaration.
  3. Final Answer:

    catch (int e) { /* handle error */ } -> Option A
  4. Quick Check:

    Correct catch syntax = catch (type var) [OK]
Hint: Catch syntax: catch (type variable) { } [OK]
Common Mistakes:
  • Omitting parentheses in catch
  • Swapping type and variable order
  • Missing variable name in catch
3. What will be the output of the following code?
try {
  throw 5;
  std::cout << "No Exception" << std::endl;
} catch (int e) {
  std::cout << "Caught exception: " << e << std::endl;
}
medium
A. Program crashes
B. No Exception
C. Caught exception: 5
D. No output

Solution

  1. Step 1: Understand the throw statement

    The code throws an integer exception with value 5, so control jumps to the catch block immediately.
  2. Step 2: Identify which catch block runs

    The catch block for int e catches the exception and prints the message with the value 5.
  3. Final Answer:

    Caught exception: 5 -> Option C
  4. Quick Check:

    Throw 5 caught by catch(int) = output message [OK]
Hint: Throw jumps to matching catch, skipping rest of try [OK]
Common Mistakes:
  • Expecting code after throw to run
  • Thinking program crashes without catch
  • Ignoring exception value in output
4. Identify the error in this code snippet:
try {
  int x = 10 / 0;
} catch (double e) {
  std::cout << "Caught double exception" << std::endl;
}
medium
A. Division by zero causes a runtime error but no exception is thrown
B. Catch block should catch int, not double
C. Try block syntax is incorrect
D. No error, code runs fine

Solution

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

    Integer division by zero causes undefined behavior or runtime error but does not throw a C++ exception automatically.
  2. Step 2: Check catch block type

    The catch block expects a double exception, but no exception is thrown, so it never runs.
  3. Final Answer:

    Division by zero causes a runtime error but no exception is thrown -> Option A
  4. Quick Check:

    Integer divide by zero = runtime error, no catch [OK]
Hint: Integer divide by zero crashes, no catch thrown [OK]
Common Mistakes:
  • Assuming division by zero throws exception
  • Mismatching catch type with thrown exception
  • Thinking try block syntax is wrong
5. Given this code, what will be the output?
try {
  try {
    throw std::string("Error");
  } catch (int e) {
    std::cout << "Caught int" << std::endl;
  }
} catch (std::string &e) {
  std::cout << "Caught string: " << e << std::endl;
}
hard
A. Caught int
B. Program crashes
C. No output
D. Caught string: Error

Solution

  1. Step 1: Trace inner try-catch

    The inner try throws a std::string exception. The inner catch expects int, so it does not catch this exception.
  2. Step 2: Trace outer catch block

    The exception propagates to the outer catch, which catches std::string &e and prints the message.
  3. Final Answer:

    Caught string: Error -> Option D
  4. Quick Check:

    Inner catch mismatch, outer catch handles string [OK]
Hint: Exception bubbles up until matching catch found [OK]
Common Mistakes:
  • Assuming inner catch catches all exceptions
  • Ignoring exception propagation
  • Expecting program crash on uncaught inner exception
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