0
0
C++programming~10 mins

Exception handling flow in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to throw an exception of type int.

C++
#include <iostream>

int main() {
    try {
        throw [1];
    } catch (int e) {
        std::cout << "Caught exception: " << e << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A5
B"error"
C3.14
Dtrue
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Throwing a string or other type that does not match the catch block.
Forgetting to use throw keyword.
2fill in blank
medium

Complete the code to catch exceptions of type std::string.

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

int main() {
    try {
        throw std::string("error");
    } catch ([1] e) {
        std::cout << "Caught string exception: " << e << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Achar
Bint
Cdouble
Dstd::string&
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Catching by value or wrong type causing no catch.
Using primitive types instead of std::string.
3fill in blank
hard

Fix the error in the catch block to properly catch any exception.

C++
#include <iostream>

int main() {
    try {
        throw 1.23;
    } catch ([1]) {
        std::cout << "Caught an exception" << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Aint e
Bdouble e
C...
Dchar e
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Specifying a wrong type in catch causing uncaught exceptions.
Missing catch block for thrown type.
4fill in blank
hard

Fill both blanks to throw and catch a const char* exception.

C++
#include <iostream>

int main() {
    try {
        throw [1];
    } catch ([2] e) {
        std::cout << "Caught message: " << e << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A"error occurred"
Bint
Cconst char*
Ddouble
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Throwing a string literal but catching as std::string.
Mismatched catch type causing uncaught exception.
5fill in blank
hard

Fill all three blanks to create a map of exception types to messages using a lambda.

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

int main() {
    std::map<std::string, std::string> errorMessages = {
        {"int", "Integer error"},
        {"double", "Double error"},
        {"string", "String error"}
    };

    auto getMessage = [&](const std::string& type) -> std::string {
        if (errorMessages.find([1]) != errorMessages.end()) {
            return errorMessages[[2]];
        } else {
            return [3];
        }
    };

    std::cout << getMessage("int") << std::endl;
    std::cout << getMessage("bool") << std::endl;

    return 0;
}
Drag options to blanks, or click blank then click option'
Atype
C"Unknown error"
D"No error"
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using wrong variable names inside the lambda.
Returning wrong default message.