0
0
C++programming~10 mins

Multiple catch blocks 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 catch an integer exception.

C++
#include <iostream>

int main() {
    try {
        throw 10;
    } catch ([1]) {
        std::cout << "Caught an integer exception." << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Astd::string e
Bint e
Cfloat e
Dchar e
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a catch type that does not match the thrown exception type.
Omitting the exception variable name.
2fill in blank
medium

Complete the code to catch a standard exception by reference.

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

int main() {
    try {
        throw std::runtime_error("Error occurred");
    } catch (const [1]& e) {
        std::cout << "Caught exception: " << e.what() << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Astd::exception
Bint
Cchar
Dfloat
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Catching by value instead of by reference.
Using a non-related type in catch.
3fill in blank
hard

Fix the error in the catch block to properly catch a double exception.

C++
#include <iostream>

int main() {
    try {
        throw 3.14;
    } catch ([1]) {
        std::cout << "Caught a double exception." << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Adouble e
Bint e
Cfloat e
Dchar e
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using int e or float e to catch a double exception.
Omitting the exception variable name.
4fill in blank
hard

Fill both blanks to catch exceptions of type int and std::string separately.

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

int main() {
    try {
        throw [1];
    } catch ([2] e) {
        std::cout << "Caught int exception: " << e << std::endl;
    } catch (std::string e) {
        std::cout << "Caught string exception: " << e << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A10
Bstd::string("error")
Cint
Dfloat
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Throwing a string but catching an int.
Mismatching catch types.
5fill in blank
hard

Fill all three blanks to catch exceptions of type float, int, and std::exception.

C++
#include <iostream>
#include <exception>

int main() {
    try {
        throw [1];
    } catch ([2] e) {
        std::cout << "Caught float exception: " << e << std::endl;
    } catch ([3] e) {
        std::cout << "Caught int exception: " << e << std::endl;
    } catch (const std::exception& e) {
        std::cout << "Caught std::exception: " << e.what() << std::endl;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A3.14f
Bfloat
Cint
Ddouble
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using double literal instead of float.
Mixing catch types order.