0

When an exception is thrown in C++, from what I understand is the stack starts unwinding and the destructors are called for each object in the functions as they are unwound.

  1. How is the exception handler found, how does it know where to stop unwinding?
  2. If the stack is unwinding how are the exceptions being passed down the stack?

Thank you

zeitue
  • 1,674
  • 2
  • 20
  • 45
  • To understand how this happens I'm trying to implement something similar, I'll add Linux and g++ is that would be helpful? – zeitue Dec 21 '15 at 20:16
  • 2
    I understand roughly how this works, but it's rather a lengthy answer, so I hope this question isn't going to be closed in the meantime... – Mats Petersson Dec 21 '15 at 20:17
  • The question that this one was marked a duplicate of does not even answer how the handler is found while unwinding – zeitue Dec 21 '15 at 20:46
  • 1
    I think it kind of does - in the sense that finding the handler is a case of unwinding the stack until you find a handler... ;) – Mats Petersson Dec 21 '15 at 20:48
  • but how would it know that a certain function handles the exception? Like maybe there is something that marks a frame as able to handle some exceptions – zeitue Dec 21 '15 at 20:49
  • Because the compiler and linker will provide information to the runtime library to understand that a particular function has an exception handler - the unwinding mechanism will then look at the exception handler specification to find if it handles THIS exception, and if it does, jump into it. If not, it will keep going, until it reaches "end of main" at which point it terminates the process. – Mats Petersson Dec 21 '15 at 20:53

0 Answers0