Bird
Raised Fist0
C++programming~10 mins

Throwing exceptions in C++ - Interactive Code Practice

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

Complete the code to throw an exception with the message "Error occurred".

C++
throw [1];
Drag options to blanks, or click blank then click option'
A"Error occurred"
Bthrow
Cstd::exception
DError occurred
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Forgetting the quotes around the error message.
Trying to throw the keyword 'throw' itself.
Throwing an undefined variable.
2fill in blank
medium

Complete the code to throw a standard exception object.

C++
throw [1]();
Drag options to blanks, or click blank then click option'
Astd::exception
Bexception
Clogic_error
Druntime_error
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Omitting the std:: namespace prefix.
Using a class that does not exist or is misspelled.
Throwing the class name without parentheses.
3fill in blank
hard

Fix the error in the code to correctly throw an integer exception.

C++
throw [1];
Drag options to blanks, or click blank then click option'
A"5"
Bint
C5
Dthrow
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using quotes around the number, making it a string.
Throwing the keyword 'throw' instead of a value.
Throwing the type name 'int' instead of a value.
4fill in blank
hard

Fill both blanks to throw a logic_error exception with a message.

C++
throw [1]("[2] error");
Drag options to blanks, or click blank then click option'
Astd::logic_error
Bruntime_error
Clogic_error
Derror
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using the class name without the std:: prefix.
Passing a variable name instead of a string message.
Using the wrong exception class.
5fill in blank
hard

Fill all three blanks to throw a custom exception with a message.

C++
class MyException : public std::exception {
public:
    const char* what() const noexcept override {
        return "[1]";
    }
};

throw [2](); // Throw the custom exception

// Catch block example
try {
    // code that throws
} catch (const [3]& e) {
    std::cout << e.what();
}
Drag options to blanks, or click blank then click option'
ACustom error occurred
BMyException
Dstd::exception
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Not overriding the what() method correctly.
Throwing or catching the wrong exception type.
Returning a non-string from what().

Practice

(1/5)
1. What does the throw keyword do in C++?
easy
A. It defines a new function.
B. It catches an exception.
C. It signals an error by raising an exception.
D. It declares a variable.

Solution

  1. Step 1: Understand the purpose of throw

    The throw keyword is used to signal that an error or exceptional condition has occurred in the program.
  2. Step 2: Differentiate from other keywords

    throw does not catch exceptions (that's catch), nor does it define functions or declare variables.
  3. Final Answer:

    It signals an error by raising an exception. -> Option C
  4. Quick Check:

    throw = signal error [OK]
Hint: Remember: throw means "raise an error" [OK]
Common Mistakes:
  • Confusing throw with catch
  • Thinking throw declares variables
  • Assuming throw defines functions
2. Which of the following is the correct syntax to throw an integer exception with value 10?
easy
A. throw int 10;
B. throw(int 10);
C. throw new int(10);
D. throw 10;

Solution

  1. Step 1: Recall correct throw syntax

    In C++, to throw an exception, you use throw followed by the value or object, without parentheses or type keywords.
  2. Step 2: Analyze each option

    throw 10; uses throw 10; which is correct. throw(int 10); incorrectly declares a type inside parentheses, invalid syntax. throw int 10; incorrectly adds type keyword. throw new int(10); throws a pointer, which is allowed but not the simplest form here.
  3. Final Answer:

    throw 10; -> Option D
  4. Quick Check:

    Simple throw syntax = throw value; [OK]
Hint: Use throw followed directly by value, no parentheses [OK]
Common Mistakes:
  • Adding parentheses after throw
  • Including type keyword with throw
  • Throwing pointers when not needed
3. What will be the output of the following code?
try {
  throw "Error occurred";
} catch (const char* msg) {
  std::cout << msg << std::endl;
}
medium
A. Error occurred
B. Runtime crash
C. No output
D. Compilation error

Solution

  1. Step 1: Understand the throw and catch block

    The code throws a string literal "Error occurred" and catches it as a const char* pointer.
  2. Step 2: Check the catch block output

    The catch block prints the caught message using std::cout, so it will output the string "Error occurred" followed by a newline.
  3. Final Answer:

    Error occurred -> Option A
  4. Quick Check:

    Thrown string caught and printed = "Error occurred" [OK]
Hint: Thrown string literals caught as const char* print directly [OK]
Common Mistakes:
  • Expecting compilation error due to throw
  • Thinking catch won't match string literal
  • Assuming no output without explicit return
4. Identify the error in this code snippet:
try {
  throw 5;
} catch (int e) {
  std::cout << "Caught: " << e << std::endl;
} catch (...) {
  std::cout << "Unknown exception" << std::endl;
}
medium
A. No error, code is correct
B. Multiple catch blocks for same exception type
C. Catch block missing parameter type
D. Throwing wrong data type

Solution

  1. Step 1: Analyze the try and catch blocks

    The code throws an integer 5, which is caught by the first catch block for int. The second catch block is a catch-all for any other exceptions.
  2. Step 2: Check for syntax or logic errors

    Multiple catch blocks are allowed and correctly ordered from specific to general. The catch blocks have correct parameter types. Throwing an int matches the first catch.
  3. Final Answer:

    No error, code is correct -> Option A
  4. Quick Check:

    Proper catch blocks for thrown int = correct code [OK]
Hint: Multiple catch blocks allowed; order matters [OK]
Common Mistakes:
  • Thinking multiple catch blocks cause error
  • Missing parameter type in catch
  • Throwing incompatible data type
5. Consider this function that throws exceptions based on input: <pre>void checkValue(int x) { if (x < 0) throw std::string("Negative value"); else if (x == 0) throw 0; else throw 3.14; } What is the best way to catch all exceptions thrown by checkValue and print a message for each type accordingly?
hard
A. Use a single catch block with catch(...) only.
B. Use separate catch blocks for std::string, int, and double types.
C. Use only catch(int) block.
D. Use try without any catch block.

Solution

  1. Step 1: Identify exception types thrown

    The function throws three different types: std::string, int, and double.
  2. Step 2: Determine how to catch each type

    To handle each exception properly, separate catch blocks for each type are needed. Using only catch(...) catches all but cannot distinguish types. Catching only int misses others. No catch block causes program termination on exception.
  3. Final Answer:

    Use separate catch blocks for std::string, int, and double types. -> Option B
  4. Quick Check:

    Multiple catch blocks for different types = best practice [OK]
Hint: Catch each thrown type separately for clear handling [OK]
Common Mistakes:
  • Using only catch(...) and missing type info
  • Catching only one exception type
  • Not using any catch block
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