Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of exception handling stack frames on the call stack

What is the order in which exception handling stack frames are pushed onto the call stack in say C#. If i have a method:

private void MyMethod() {
  try {
    DoSomething();  
  }
  catch (Exception ex)
  {
    //Handle
  }
}

Is a separate stack frame created for each exception handler as follows?

DoSomething stackframe<br/>
Exception stackframe<br/>
MyMethod stackframe<br/>

OR

DoSomething stackframe<br />
MyMethod stackframe<br />
Exception stackframe<br />

OR

something else?

like image 577
intermension Avatar asked May 18 '26 23:05

intermension


1 Answers

No, adding an exception handler doesn't add a new frame to the call stack. It just adds appropriate information so that when an exception is thrown, at each level of the call stack the framework can find the appropriate handler for that exception (if indeed there is an appropriate handler).

It's a little bit like garbage collection, where at any point of execution the GC can work out which local variables should still count as GC roots - essentially there's more to a method than the executable code itself :)

like image 144
Jon Skeet Avatar answered May 21 '26 23:05

Jon Skeet