Difficulty: Easy
Correct Answer: A run-time error.
Explanation:
Introduction / Context:
In C++ programming, it is vital to distinguish between compile-time errors (like syntax mistakes) and run-time errors (like division by zero, out-of-range access, or failed resource acquisition). Exceptions are a language mechanism intended to signal and handle exceptional conditions that usually occur at execution time, not during compilation. This question asks which problem class typically triggers an exception.
Given Data / Assumptions:
Concept / Approach:
Exceptions are raised while the program is running. A compile-time error such as a missing semicolon is caught by the compiler and prevents the program from running at all. Syntax errors are therefore not associated with exceptions. Library and user code can throw exceptions to report problems that emerge only with real data and state at run time. These can be handled by catch blocks to recover or terminate gracefully.
Step-by-Step Solution:
Verification / Alternative check:
Try compiling code with a missing semicolon: compilation fails, and the binary is not produced. By contrast, code that throws (e.g., throw std::runtime_error("x");) compiles successfully and triggers an exception only when executed within a try block or propagates to terminate if unhandled.
Why Other Options Are Wrong:
Missing semicolon in main(): a classic syntax error detected at compile time.
A syntax error: all syntax errors are compile-time issues and do not involve exceptions.
A problem in calling function: too vague; it could be anything. Only if it occurs at run time and throws would it be relevant.
Common Pitfalls:
Final Answer:
A run-time error.
Discussion & Comments