0

I've heard the phrase:

"let the call stack unwind more than one level"

being mentioned in exception handling.

What does this mean?

Language in question is C#.

Thanks

GurdeepS
  • 65,107
  • 109
  • 251
  • 387
  • What's the source of this phrase? Do you have a reference that could provide some context? – James Johnston Jan 13 '12 at 00:32
  • possible duplicate of [.Net - what is an "unwind"?](http://stackoverflow.com/questions/2005606/net-what-is-an-unwind) – Cody Gray - on strike Jan 13 '12 at 00:35
  • @Cody I'm not sure that this question is an _exact_ dup. – Matt Ball Jan 13 '12 at 00:41
  • @Matt: Why not? The question is phrased a little differently, yes, but the answers seem perfectly applicable. Perhaps some of them could be better, but they should be edited and posted there, not in multiple places. – Cody Gray - on strike Jan 13 '12 at 00:45
  • @Cody not going to make a big stink about it either way, but my general understanding is that the question itself, not the (possible) resultant answers, make a dup. I think ChrisF's comment on [this question on meta](http://meta.stackexchange.com/questions/95799/closing-as-duplicate-when-the-answers-are-duplicates) illustrates the point nicely, though that entire q&a is relevant, IMO. – Matt Ball Jan 13 '12 at 00:51

3 Answers3

1

In simplistic terms, you can think of the call stack working as follows:

  • Every time a method/function is called, a "frame" is pushed onto the stack.
  • Every time a method/function returns/finishes/exits, that "frame" is popped off the stack.

So, when the call stack unwinds more than one level, more than one stack frame comes off at once — like a series of nested method calls (not just a single method call) returning.

In terms of exceptions:

  • Throwing an exception which is the caught within the same method will not cause the function to return prematurely, so the stack is not "unwound" any levels at all.
  • Throwing an exception which is caught in the direct caller/parent method of the method that threw the exception will cause that child method to return prematurely, but not the parent method, so exactly one stack frame will be popped.
  • Throwing an exception which is not caught until it has caused numerous method calls to exit prematurely will "let the call stack unwind more than one level."

Apologies for being a bit fast-and-loose with the terminology.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
0

This means to let the exception exit the function that threw it and allow its caller to catch the function and deal with it.

JKor
  • 3,822
  • 3
  • 28
  • 36
0

As a general principle it is best to let the lowest possible method on the stack (specifically where it still has meaning or something can be done with it) catch an exception. This reduces redundant and otherwise unnecessary try...catch blocks and lets the code that can most appropriately handle an exception actually contain the logic to do so.

Update

Consider the following fairly general (and a bit contrived) example:

public abstract class ThrowRandomIOExceptions
{
    public void SafeIOOperation(string fileName, IModel model)
    {
        try
        {
            DoSomeIOOperation(fileName, model);
        }
        catch(FileNotFoundException ex)
        {
            // Handle the exception so the children don't have to.
        }
    }

    public abstract void DoSomeIOOperation(string fileName,  IModel model);
}

public class WriteCSV : ThrowRandomIOExceptions
{
    public void DoSomeIOOperation(string fileName, IModel model)
    {
        // Write the model in comma delimited format
    }
}

public class WriteTSV : ThrowRandomIOExceptions
{
    public void DoSomeIOOperation(string fileName, IModel model)
    {
        // Write the model in tab delimited format
    }
}

Handling the FileNotFoundException in the base class avoids the children from having to include their own try...catch and provides them the guarantee that exceptions from improper file names will be handled.

M.Babcock
  • 18,753
  • 6
  • 54
  • 84