C++ Exception / Error Handling - MCQs

1 min read

1. What is exception handling in C++?

a) A mechanism to respond to exceptional conditions
b) A way to define variables
c) A loop construct
d) A memory allocator
Correct Answer: a) A mechanism to respond to exceptional conditions
Explanation:
Exception handling separates error response code from normal control flow.

2. Which keyword begins a protected block that can catch exceptions?

a) catch
b) try
c) throw
d) finally
Correct Answer: b) try
Explanation:
A try block encloses code that may throw exceptions.

3. Which keyword defines a handler for an exception?

a) try
b) throw
c) catch
d) handle
Correct Answer: c) catch
Explanation:
catch blocks handle exceptions of matching types.

4. Which keyword raises an exception?

a) catch
b) try
c) handle
d) throw
Correct Answer: d) throw
Explanation:
throw transfers control to an exception handler.

5. What does a throw expression do?

a) Transfers control to a matching handler
b) Ends the program immediately
c) Restarts the function
d) Deletes variables
Correct Answer: a) Transfers control to a matching handler
Explanation:
throw begins exception propagation.

6. What happens if no catch block matches an exception?

a) It is ignored
b) The exception propagates to the caller
c) The object is deleted
d) The program loops
Correct Answer: b) The exception propagates to the caller
Explanation:
Uncaught exceptions move up the call stack.

7. What happens if no handler handles an exception?

a) The program continues
b) The exception is retried
c) std::terminate is called
d) The catch block runs anyway
Correct Answer: c) std::terminate is called
Explanation:
Unhandled exceptions cause termination by default.

8. Can an int be thrown as an exception in C++?

a) No
b) Only in templates
c) Only if const
d) Yes
Correct Answer: d) Yes
Explanation:
C++ allows almost any type to be thrown.

9. What does catch(...) do?

a) Catches any exception
b) Catches only int
c) Catches only std::exception
d) Catches no exceptions
Correct Answer: a) Catches any exception
Explanation:
The ellipsis handler matches all exception types.

10. In what order should catch blocks usually be written?

a) Base classes first
b) Most derived classes first
c) Random order
d) Alphabetical order
Correct Answer: b) Most derived classes first
Explanation:
Catch derived types before base types to avoid hiding them.

11. What is an exception object?

a) A loop counter
b) A class destructor
c) The object thrown and caught
d) A pointer to main
Correct Answer: c) The object thrown and caught
Explanation:
The thrown value is stored during stack unwinding.

12. What is a common best practice for throwing and catching exceptions?

a) Throw pointers only
b) Catch by value always
c) Throw arrays
d) Throw by value and catch by reference
Correct Answer: d) Throw by value and catch by reference
Explanation:
This avoids slicing and lifetime problems.

13. What is stack unwinding?

a) Destroying local objects while an exception propagates
b) Deleting global variables
c) Restarting the program
d) Inlining functions
Correct Answer: a) Destroying local objects while an exception propagates
Explanation:
Destructors for constructed local objects run during unwinding.

14. What does RAII help with during exceptions?

a) Avoiding constructors
b) Automatic resource cleanup
c) Faster loops
d) Static typing
Correct Answer: b) Automatic resource cleanup
Explanation:
RAII releases resources in destructors during unwinding.

15. What does std::exception provide?

a) A loop
b) A memory pool
c) A base class with what()
d) A thread handler
Correct Answer: c) A base class with what()
Explanation:
Standard exceptions derive from std::exception.

16. What does what() return?

a) An int
b) A bool
c) A std::string always
d) A const char pointer message
Correct Answer: d) A const char pointer message
Explanation:
what provides implementation-defined error text.

17. What is std::runtime_error?

a) A class derived from std::exception
b) A keyword
c) A macro
d) A compiler flag
Correct Answer: a) A class derived from std::exception
Explanation:
runtime_error represents errors detected at runtime.

18. What is std::logic_error?

a) A loop
b) A class derived from std::exception
c) A destructor
d) A stream
Correct Answer: b) A class derived from std::exception
Explanation:
logic_error represents errors in program logic.

19. What is std::invalid_argument derived from?

a) std::runtime_error
b) std::bad_alloc
c) std::logic_error
d) std::ostream
Correct Answer: c) std::logic_error
Explanation:
invalid_argument reports invalid function arguments.

20. What is std::out_of_range derived from?

a) std::runtime_error
b) std::bad_cast
c) std::iostream
d) std::logic_error
Correct Answer: d) std::logic_error
Explanation:
out_of_range reports arguments outside valid ranges.

21. Which exception is commonly thrown by new when allocation fails?

a) std::bad_alloc
b) std::logic_error
c) std::invalid_argument
d) std::overflow_error
Correct Answer: a) std::bad_alloc
Explanation:
new throws bad_alloc when memory cannot be allocated.

22. What is std::overflow_error derived from?

a) std::logic_error
b) std::runtime_error
c) std::bad_alloc
d) std::exception only
Correct Answer: b) std::runtime_error
Explanation:
overflow_error reports arithmetic overflow.

23. What is std::underflow_error derived from?

a) std::logic_error
b) std::bad_cast
c) std::runtime_error
d) std::system_error
Correct Answer: c) std::runtime_error
Explanation:
underflow_error reports arithmetic underflow.

24. What is std::length_error derived from?

a) std::runtime_error
b) std::bad_alloc
c) std::iostream
d) std::logic_error
Correct Answer: d) std::logic_error
Explanation:
length_error reports attempts to exceed maximum size.

25. Can a throw expression create a temporary exception object?

a) Yes
b) No
c) Only for pointers
d) Only for arrays
Correct Answer: a) Yes
Explanation:
throw can construct a temporary object.

26. Which catch parameter is usually recommended for standard exceptions?

a) std::exception e
b) catch (const std::exception& e)
c) std::exception* e only
d) int e
Correct Answer: b) catch (const std::exception& e)
Explanation:
Const reference avoids slicing and allows polymorphism.

27. What can happen if you catch a polymorphic exception by value?

a) It improves safety
b) It is required
c) Object slicing
d) Faster unwinding
Correct Answer: c) Object slicing
Explanation:
Catching by value can copy only the base part.

28. What is true about catch (std::exception e)?

a) It is impossible
b) It is always best
c) It prevents slicing
d) It can cause object slicing
Correct Answer: d) It can cause object slicing
Explanation:
Derived exception data may be lost.

29. How can you rethrow the current exception inside a catch block?

a) throw;
b) rethrow;
c) throw current;
d) continue;
Correct Answer: a) throw;
Explanation:
A bare throw rethrows the current exception.

30. What happens if throw; is executed outside a catch block?

a) Nothing
b) It calls std::terminate
c) It returns null
d) It restarts the try block
Correct Answer: b) It calls std::terminate
Explanation:
There is no current exception to rethrow.

31. Can constructors throw exceptions?

a) Never
b) Only destructors can
c) Yes
d) Only in C++98
Correct Answer: c) Yes
Explanation:
Constructors can report initialization failure by throwing.

32. What happens if a constructor throws an exception?

a) The object destructor is always called
b) No cleanup occurs
c) The object remains valid
d) Fully constructed subobjects are destroyed
Correct Answer: d) Fully constructed subobjects are destroyed
Explanation:
The object itself was never fully constructed.

33. Should destructors usually throw exceptions?

a) No
b) Yes always
c) Only during construction
d) Only if private
Correct Answer: a) No
Explanation:
Throwing destructors can cause termination during unwinding.

34. What can happen if a destructor throws during stack unwinding?

a) It is ignored
b) It may cause std::terminate
c) It restarts the program
d) It improves cleanup
Correct Answer: b) It may cause std::terminate
Explanation:
Multiple active exceptions are unsafe.

35. What does noexcept mean?

a) The function is virtual
b) The function is deleted
c) The function is not expected to throw
d) The function is inline
Correct Answer: c) The function is not expected to throw
Explanation:
noexcept supports optimization and safety contracts.

36. What happens if an exception leaves a noexcept function?

a) It is caught automatically
b) It is retried
c) It becomes a warning
d) std::terminate is called
Correct Answer: d) std::terminate is called
Explanation:
Violating noexcept ends the program by default.

37. What does the noexcept operator return?

a) A Boolean
b) A string
c) An iterator
d) A pointer
Correct Answer: a) A Boolean
Explanation:
It checks whether an expression is declared noexcept.

38. What does noexcept(false) indicate?

a) It guarantees no exceptions
b) It indicates the function may throw
c) It deletes the function
d) It makes the class abstract
Correct Answer: b) It indicates the function may throw
Explanation:
noexcept(false) is the absence of a no-throw guarantee.

39. Are destructors noexcept by default in modern C++?

a) No
b) Only virtual ones
c) Yes
d) Only if user-declared
Correct Answer: c) Yes
Explanation:
Destructors are implicitly noexcept unless specified otherwise.

40. Can move constructors be noexcept?

a) No
b) Only if virtual
c) Only if deleted
d) Yes, and it can improve container behavior
Correct Answer: d) Yes, and it can improve container behavior
Explanation:
Containers may prefer noexcept move operations.

41. When is std::terminate commonly called?

a) Unhandled exceptions or noexcept violations
b) Every catch block
c) Every throw expression
d) Every constructor
Correct Answer: a) Unhandled exceptions or noexcept violations
Explanation:
terminate ends abnormal exception situations.

42. Which function can replace the terminate handler?

a) std::set_new_handler
b) std::set_terminate
c) std::set_exception
d) std::set_catch
Correct Answer: b) std::set_terminate
Explanation:
set_terminate installs a custom termination handler.

43. What is exception safety?

a) Avoiding all functions
b) Using goto
c) Keeping objects valid and resources safe when exceptions occur
d) Disabling destructors
Correct Answer: c) Keeping objects valid and resources safe when exceptions occur
Explanation:
Exception-safe code maintains invariants during failures.

44. Which guarantee means no changes occur if an exception is thrown?

a) Basic guarantee
b) Nothrow guarantee
c) Weak guarantee
d) Strong exception guarantee
Correct Answer: d) Strong exception guarantee
Explanation:
Operations succeed completely or have no effect.

45. Which guarantee says objects remain valid after an exception?

a) Basic exception guarantee
b) Nothrow guarantee
c) Compile-time guarantee
d) Link-time guarantee
Correct Answer: a) Basic exception guarantee
Explanation:
Resources are not leaked and invariants remain valid.

46. Which guarantee means a function never throws?

a) Strong guarantee
b) Nothrow guarantee
c) Basic guarantee
d) Partial guarantee
Correct Answer: b) Nothrow guarantee
Explanation:
Nothrow operations must not throw exceptions.

47. Which idiom is commonly used for exception-safe assignment?

a) Global swap
b) Goto cleanup
c) Copy-and-swap
d) Manual delete
Correct Answer: c) Copy-and-swap
Explanation:
It provides strong safety by working on a copy.

48. What is a scope guard?

a) A loop condition
b) A namespace
c) A template only
d) An RAII cleanup helper
Correct Answer: d) An RAII cleanup helper
Explanation:
Scope guards run cleanup when scope exits.

49. What is the purpose of a try block?

a) To enclose code that may throw
b) To declare variables only
c) To define macros
d) To replace functions
Correct Answer: a) To enclose code that may throw
Explanation:
try groups code with associated handlers.

50. Can a try block execute normally without throwing?

a) No
b) Yes
c) Only if empty
d) Only with catch(...)
Correct Answer: b) Yes
Explanation:
try blocks do not require an exception to occur.

51. Can a catch block throw another exception?

a) No
b) Only integers
c) Yes
d) Only std::exception
Correct Answer: c) Yes
Explanation:
Handlers can translate or wrap exceptions.

52. Can a catch block finish by returning normally?

a) No
b) Only if rethrowing
c) Only with throw;
d) Yes
Correct Answer: d) Yes
Explanation:
A handler can recover and continue execution.

53. What did an empty dynamic exception specification throw() mean in older C++?

a) The function should not throw exceptions
b) The function throws all exceptions
c) The function is deleted
d) The function is private
Correct Answer: a) The function should not throw exceptions
Explanation:
throw() was an early no-exception specification.

54. What happened to dynamic exception specifications in C++11?

a) They became mandatory
b) They were deprecated
c) They were improved
d) They became virtual
Correct Answer: b) They were deprecated
Explanation:
Modern C++ prefers noexcept.

55. What happened to dynamic exception specifications in C++17?

a) They became required
b) They became faster
c) They were removed, but throw() remained as deprecated noexcept
d) They became catch blocks
Correct Answer: c) They were removed, but throw() remained as deprecated noexcept
Explanation:
Dynamic specifications were removed from the language.

56. Can nullptr be thrown?

a) No
b) Only as int
c) Only in C
d) Yes
Correct Answer: d) Yes
Explanation:
Pointer values can be thrown.

57. Can an enum value be thrown?

a) Yes
b) No
c) Only if constexpr
d) Only if public
Correct Answer: a) Yes
Explanation:
Enums are valid exception types.

58. Can a char be caught?

a) No
b) Yes
c) Only in templates
d) Only with new
Correct Answer: b) Yes
Explanation:
Any thrown type can have a matching catch.

59. Can a function have multiple catch blocks?

a) No
b) Only one
c) Yes
d) Only with macros
Correct Answer: c) Yes
Explanation:
Multiple handlers can handle different exception types.

60. What is true about catch(...)?

a) It names the exception object
b) It gives the exception type
c) It gives the exception message
d) It catches without naming the exception object
Correct Answer: d) It catches without naming the exception object
Explanation:
The ellipsis handler does not provide a parameter name.

61. Which function captures the current exception into an exception_ptr?

a) std::current_exception
b) std::make_exception
c) std::throw_ptr
d) std::catch_ptr
Correct Answer: a) std::current_exception
Explanation:
It stores an exception for later rethrowing.

62. What can std::exception_ptr store?

a) Only integers
b) A stored exception
c) A lambda only
d) A container only
Correct Answer: b) A stored exception
Explanation:
exception_ptr can hold an exception across scopes.

63. Which function rethrows a stored exception_ptr?

a) std::throw
b) std::exception_ptr
c) std::rethrow_exception
d) std::current_exception
Correct Answer: c) std::rethrow_exception
Explanation:
It throws the exception stored in an exception_ptr.

64. Which function creates an exception_ptr from an exception object?

a) std::current_exception
b) std::rethrow_exception
c) std::catch_exception
d) std::make_exception_ptr
Correct Answer: d) std::make_exception_ptr
Explanation:
It constructs and stores an exception object.

65. How can exceptions be transferred between threads?

a) Using std::exception_ptr
b) Using goto
c) Using global int only
d) Using macros
Correct Answer: a) Using std::exception_ptr
Explanation:
exception_ptr can be passed across threads.

66. Which function returns the number of active uncaught exceptions?

a) std::terminate
b) std::uncaught_exceptions
c) std::current_exception
d) std::rethrow_exception
Correct Answer: b) std::uncaught_exceptions
Explanation:
It returns the active exception count.

67. What is true about std::uncaught_exception without the final s?

a) It is preferred
b) It is mandatory
c) It is deprecated
d) It is virtual
Correct Answer: c) It is deprecated
Explanation:
std::uncaught_exceptions is the modern alternative.

68. Should exceptions be used for normal control flow?

a) Always
b) Only for loops
c) They are required
d) Generally discouraged
Correct Answer: d) Generally discouraged
Explanation:
Exceptions are best for exceptional conditions.

69. Which is an alternative to exceptions for error handling?

a) Return codes, optional, or expected-like types
b) Deleting constructors
c) Avoiding destructors
d) Using only macros
Correct Answer: a) Return codes, optional, or expected-like types
Explanation:
Not all errors require exceptions.

70. Which type can represent a value that may be absent?

a) std::vector
b) std::optional
c) std::map
d) std::queue
Correct Answer: b) std::optional
Explanation:
optional can model missing values without exceptions.

71. Which C++23 type can represent a value or an error?

a) std::pair only
b) std::tuple only
c) std::expected
d) std::stack
Correct Answer: c) std::expected
Explanation:
expected stores either a result or an error.

72. Which C-compatible error indicator is often used with system functions?

a) catch
b) throw
c) try
d) errno
Correct Answer: d) errno
Explanation:
errno stores error codes for many C APIs.

73. Which macro checks a runtime assumption and may abort?

a) assert
b) static_assert
c) noexcept
d) throw
Correct Answer: a) assert
Explanation:
assert tests conditions during debugging.

74. Which construct checks conditions at compile time?

a) assert
b) static_assert
c) try
d) catch
Correct Answer: b) static_assert
Explanation:
static_assert fails compilation if false.

75. Which macro can disable assert checks?

a) DEBUG
b) ASSERT_ON
c) NDEBUG
d) NO_ASSERT
Correct Answer: c) NDEBUG
Explanation:
NDEBUG removes assert checks.

76. What must static_assert use?

a) A runtime loop
b) A pointer
c) A stream
d) A constant expression
Correct Answer: d) A constant expression
Explanation:
Compile-time checks require constant expressions.

77. Can allocation failure cause exceptions in C++?

a) Yes, through std::bad_alloc
b) No
c) Only in C
d) Only with malloc
Correct Answer: a) Yes, through std::bad_alloc
Explanation:
new reports allocation failure with exceptions by default.

78. Which form of new returns nullptr instead of throwing?

a) new fast
b) new (std::nothrow)
c) new null
d) new quiet
Correct Answer: b) new (std::nothrow)
Explanation:
nothrow new returns nullptr on failure.

79. Does malloc throw exceptions?

a) Yes always
b) Only in C++
c) No, it returns nullptr on failure
d) Only for arrays
Correct Answer: c) No, it returns nullptr on failure
Explanation:
malloc is a C allocation function.

80. How should allocation failures be handled in C++?

a) Always ignored
b) Only with goto
c) Only with assert
d) Handle exceptions or use nothrow depending on design
Correct Answer: d) Handle exceptions or use nothrow depending on design
Explanation:
Choose error handling that matches system requirements.

81. Which exception can dynamic_cast throw for references?

a) std::bad_cast
b) std::bad_alloc
c) std::invalid_argument
d) std::overflow_error
Correct Answer: a) std::bad_cast
Explanation:
Reference dynamic_cast throws on failure.

82. What does dynamic_cast return on pointer failure?

a) An exception always
b) nullptr
c) 0.0
d) true
Correct Answer: b) nullptr
Explanation:
Pointer dynamic_cast returns nullptr when it fails.

83. Which exception can typeid throw for a null polymorphic pointer?

a) std::bad_alloc
b) std::logic_error
c) std::bad_typeid
d) std::length_error
Correct Answer: c) std::bad_typeid
Explanation:
typeid on a null polymorphic pointer can throw.

84. Which exception may be thrown by calling an empty std::function?

a) std::bad_alloc
b) std::invalid_argument
c) std::runtime_error
d) std::bad_function_call
Correct Answer: d) std::bad_function_call
Explanation:
Calling a targetless function object throws.

85. Which class wraps operating-system error information?

a) std::system_error
b) std::logic_error
c) std::bad_cast
d) std::length_error
Correct Answer: a) std::system_error
Explanation:
system_error carries an error_code.

86. Which type stores an error value and category?

a) std::exception
b) std::error_code
c) std::string
d) std::vector
Correct Answer: b) std::error_code
Explanation:
error_code represents system-specific errors.

87. Which type provides a portable error condition?

a) std::error_code
b) std::bad_alloc
c) std::error_condition
d) std::exception_ptr
Correct Answer: c) std::error_condition
Explanation:
error_condition abstracts error categories.

88. Which enumeration defines portable error conditions?

a) std::exception
b) std::error_code
c) std::system_error
d) std::errc
Correct Answer: d) std::errc
Explanation:
errc lists common error conditions.

89. Which exception can vector::at throw?

a) std::out_of_range
b) std::bad_alloc only
c) std::invalid_argument only
d) std::system_error
Correct Answer: a) std::out_of_range
Explanation:
at performs bounds checking.

90. Which exception can map::at throw when a key is missing?

a) std::bad_cast
b) std::out_of_range
c) std::overflow_error
d) std::bad_alloc
Correct Answer: b) std::out_of_range
Explanation:
map::at reports missing keys.

91. Which exception can std::stoi throw if conversion fails?

a) std::bad_alloc
b) std::overflow_error
c) std::invalid_argument
d) std::length_error
Correct Answer: c) std::invalid_argument
Explanation:
stoi throws when no conversion can be performed.

92. Which exception can std::stoi throw if the value is too large?

a) std::invalid_argument
b) std::bad_cast
c) std::system_error
d) std::out_of_range
Correct Answer: d) std::out_of_range
Explanation:
stoi throws when the number is out of int range.

93. Which exception can std::string::substr throw for a bad position?

a) std::out_of_range
b) std::bad_alloc
c) std::invalid_argument
d) std::length_error
Correct Answer: a) std::out_of_range
Explanation:
substr checks the starting position.

94. Which exception can std::string::at throw for a bad index?

a) std::bad_cast
b) std::out_of_range
c) std::invalid_argument
d) std::overflow_error
Correct Answer: b) std::out_of_range
Explanation:
at checks character indexes.

95. How should exceptions usually be caught?

a) By value only
b) By pointer only
c) By const reference
d) By macro
Correct Answer: c) By const reference
Explanation:
Const reference preserves type and avoids slicing.

96. What should custom exception classes usually derive from?

a) std::vector
b) std::string
c) std::ostream
d) std::exception
Correct Answer: d) std::exception
Explanation:
Deriving from std::exception integrates with handlers.

97. What should a custom exception class usually override?

a) what()
b) main()
c) size()
d) begin()
Correct Answer: a) what()
Explanation:
what provides the error message.

98. What is a good property of exception objects?

a) Mutable
b) Immutable
c) Global
d) Virtual only
Correct Answer: b) Immutable
Explanation:
Exceptions should carry stable error information.

99. What does an exception hierarchy allow?

a) No handlers
b) Only exact catches
c) Catch broad or specific handlers
d) No polymorphism
Correct Answer: c) Catch broad or specific handlers
Explanation:
Hierarchies support flexible error handling.

100. What is a best practice for C++ error handling?

a) Use exceptions for all loop control
b) Ignore allocation errors
c) Throw from destructors
d) Use exceptions for exceptional errors with RAII
Correct Answer: d) Use exceptions for exceptional errors with RAII
Explanation:
RAII and exception safety keep programs robust.
← Previous: C++ Classes and Objects - MCQs
Next →: C++ Inheritance and Polymorphism - MCQs
New
No Image

C++ Templates - MCQs

1. What is exception handling in C++? a) A mechanism to respond to exceptional conditions b) A way to define…

By MCQs Generator
NewLatest C++ Basics MCQs

Latest C++ Basics MCQs

Created by Bjarne Stroustrup as an extension of the C programming language, C++ is a powerful, high-performance, statically typed language…

By MCQs Generator
New
No Image

C++ Inheritance and Polymorphism - MCQs

1. What is exception handling in C++? a) A mechanism to respond to exceptional conditions b) A way to define…

By MCQs Generator