public class CallStack
{
public void Test()
{
try
{
this.A();
}
catch( Exception e )
{
Console.WriteLine( e.StackTrace );
}
}
protected void A()
{
this.B();
}
protected void B()
{
try
{
this.C();
}
catch( Exception /*e*/ )
{
throw;
}
}
protected void C()
{
this.D();
}
protected void D()
{
try
{
this.E();
}
catch( Exception e )
{
throw e;
}
}
protected void E()
{
this.Throw();
}
protected void Throw()
{
throw new Exception( "An Exception occurred" );
}
}
VulpesPosted May 15, 2013, 5:24 AM
D
B
Test
The methods A, C, E and Throw will all be 'inlined' (i.e they will be eliminated by including their code in the method which calls them) and so are superfluous.
This can be seen by changing the code as follows which produces exactly the same output:
Abhishek SurPosted May 15, 2013, 12:07 AM
Throw =>E=>D=>C=>B=>A=>Test
in stack until the exception is thrown.
When exception is thrown, it moves to D(), which has throw e.
Now throw e removes the CallStack present on the exception object until now and creates a new stack. So when it reaches B, it will have only :
D=>C=>B=>A=>Test
Thereby removing the rest of the stack that has been kept track from down the whole call stack.
Finally, it gets throw which does not create a new Exception object but rather it throws the existing.
Remember, Throw ex does not preserve CallStack but Throw does. Exception.StackTrace is mutable.