Recently an interviewer asked me where the exception object in C++ is allocated, heap or stack? I'm not sure but I answered stack since I thought there is no "new" or "malloc". Is it correct?
Then he kept asking me that if it's on stack, assuming class A throw a exception object, let's say "e", and class B catch "e". Since "e" is on the stack of A, then how does B can have the access to this "e"?
I'm not very clear about the second question. Could anyone can give me some example code showing that "class A throw e and class B catch it"? Also, I guessed B can catch e by copying the value or address but the interviewer only denied my answer without giving me right one, so what is the right answer, is there any mechanism can ensure class object can catch exceptions from other class objects? Thanks~
Basically yes, though exception handling is a bit special because it unwinds the stack for
throw
operations.The local scope of
is somehow equivalent as looking to a
local scope
and matching that kind oflocal scope
with the best matchingcatch(...)
statement.From [except.throw]/15.1/4:
The final reference, [basic.stc.dynamic.allocation]/4, says:
throw new std::exception vs throw std::exception
The above link has a pretty good answer.
I think the answer would be "usually" heap since you are throwing an object which would be located on the heap but if it is a static object (not sure if such a thing exists) then it would be on the stack.
It can't be stack as the when exception is thrown stack unwinds and you'd lose the exception object if allocated in the frame that caused the exception.
I remember reading something about it in C++ Primer, 5Ed. It said
And looking at @Kerrek's asnwer above along with it, I believe it's a separate space allocated and is specific to compilers.