0
0
C++programming~20 mins

Throwing exceptions in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Exception Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this C++ code with exception throwing?

Consider the following C++ program. What will it print when run?

C++
#include <iostream>
#include <stdexcept>

int main() {
    try {
        throw std::runtime_error("Error happened");
        std::cout << "No error" << std::endl;
    } catch (const std::runtime_error& e) {
        std::cout << "Caught: " << e.what() << std::endl;
    }
    return 0;
}
ACaught: Error happened
BNo error
CCaught: std::runtime_error
DProgram crashes with uncaught exception
Attempts:
2 left
πŸ’‘ Hint

Remember that throwing an exception transfers control to the matching catch block.

❓ Predict Output
intermediate
2:00remaining
What error does this code produce?

What happens when this C++ code runs?

C++
#include <iostream>
#include <string>

void f() {
    throw 42;
}

int main() {
    try {
        f();
    } catch (const std::string& e) {
        std::cout << "Caught string: " << e << std::endl;
    }
    return 0;
}
ANo output, program runs normally
BCaught string: 42
CCompilation error due to catch type mismatch
DProgram terminates with uncaught exception
Attempts:
2 left
πŸ’‘ Hint

Check if the catch block matches the thrown exception type.

πŸ”§ Debug
advanced
2:00remaining
Why does this code fail to catch the exception?

Identify the reason the exception is not caught in this C++ code snippet.

C++
#include <iostream>
#include <stdexcept>

int main() {
    try {
        throw "Error";
    } catch (const std::exception& e) {
        std::cout << "Caught exception: " << e.what() << std::endl;
    }
    return 0;
}
ACannot catch a const char* with std::exception reference
BMissing semicolon after throw statement
Cstd::exception does not have what() method
DThrowing string literals is not allowed
Attempts:
2 left
πŸ’‘ Hint

Think about the type of the thrown object and the catch parameter type.

❓ Predict Output
advanced
2:00remaining
What is the output of nested try-catch with rethrow?

What will this C++ program print?

C++
#include <iostream>
#include <stdexcept>

int main() {
    try {
        try {
            throw std::logic_error("Logic error");
        } catch (const std::logic_error& e) {
            std::cout << "Inner catch: " << e.what() << std::endl;
            throw;  // rethrow
        }
    } catch (const std::exception& e) {
        std::cout << "Outer catch: " << e.what() << std::endl;
    }
    return 0;
}
AOuter catch: Logic error
BInner catch: Logic error
C
Inner catch: Logic error
Outer catch: Logic error
DProgram crashes with uncaught exception
Attempts:
2 left
πŸ’‘ Hint

Remember that throw; rethrows the current exception to the outer catch block.

🧠 Conceptual
expert
2:00remaining
Which option correctly describes exception object slicing in C++?

Consider throwing a derived class exception object but catching by value as a base class. What happens?

AThe catch block receives the full derived class object with all info intact
BThe caught exception object is sliced to base class, losing derived class info
CThe program crashes due to slicing at runtime
DCatching by value is not allowed in C++
Attempts:
2 left
πŸ’‘ Hint

Think about what happens when an object is copied by value from a derived to a base class type.