Q
In a C++ try block that can throw a Derived exception (where Derived inherits from Base), what is the correct order of catch blocks?
-
A
catch(Derived&) before catch(Base&), with catch(...) last
-
B
catch(...) first
-
C
catch(Base&) before catch(Derived&)
-
D
Order does not matter
Correct Answer:
A. catch(Derived&) before catch(Base&), with catch(...) last
Explanation:
Handlers are tried in order. A Base handler placed first would also catch Derived exceptions, leaving the Derived handler unreachable.