// Recursive helper method to build a callstack of 30 frames, which // is used to rethrow an exception at each level static void BuildUpCallStack(object data) { ThreadStartParameter parameter = (ThreadStartParameter)data; if (parameter.CallStackDepth < 30) { try { BuildUpCallStack(new ThreadStartParameter { Id = parameter.Id, CallStackDepth = parameter.CallStackDepth + 1, ThrowException = parameter.ThrowException }); } catch (MyException e) { Log(string.Format("Caught exception callstack frame {0}", parameter.CallStackDepth)); // If an exception occured, rethrow it unless this // callstack frame was the first if (parameter.CallStackDepth != 0) { throw; } // If this frame was the first in callstack, restart the // thread but this time without throwing an exception (for // demonstrate such a restart character like your Proxies do) BuildUpCallStack(new ThreadStartParameter { Id = parameter.Id, CallStackDepth = 0, ThrowException = false }); } return; } DoSomething(parameter); }
static void DoSomething(object data) { ThreadStartParameter parameter = (ThreadStartParameter)data; for (int counter = 0; counter < 7; counter++) { if (counter == 3 && parameter.ThrowException) { throw new MyException(); } Log(parameter.Id); Thread.Sleep(100); } }