ACtE03 · Chapter 3Programming & Applications🏛️ Past Licensure QuestionID: 8464d99e
In C++, how are custom user-defined exceptions conventionally implemented?
In C++, how are custom user-defined exceptions conventionally implemented?
Multiple Choice Options
- A
By modifying the compiler source code
- B✓ Correct Option
By inheriting from
std::exceptionand overriding itswhat()virtual method - C
By declaring global macros
- D
By creating private global pointers
Detailed Technical Solution & Explanation
In C++, the standard approach to define a custom exception is to create a class that derives publicly from std::exception (or one of its standard subclasses like std::runtime_error) and override the virtual member function const char* what() const noexcept.
Option Analysis:
•
Option A (By modifying the compiler source code): Unnecessary and incorrect.
•
Option B (exception and overriding what): Correct. Standard C++ idiom for user-defined exceptions.
•
Option C (By declaring global macros): Macros cannot emulate polymorphic exception hierarchies.
•
Option D (By creating private global pointers): Irrelevant to exception classes.
Reference: Mock Test 03NEC Licensure Syllabus