实际上你可以捕捉到 Stackoverflow 执行,当然递归方法必须配合你做一个这样的方法:
void Zoo()
{
RuntimeHelpers.EnsureSufficientExecutionStack();
int[] baba = new int[1024 * 5];
Zoo();
}
然后像这样调用它
try
{
Zoo();
}
//catch (Exception ex)
catch(InsufficientExecutionStackException ex)
{
ex.ProcessException().Show("Good God what are you doing");
}
这就是进程异常方法的工作原理
public static class Helper{
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern uint GetCurrentThreadId();
public static string ProcessException(this Exception ex)
{
StringBuilder strBuild = new StringBuilder(5000);
if (ex is InsufficientExecutionStackException)
{
strBuild.AppendLine("#%#%#%#%#% We Ran out of Stack Space on thread id : " + GetCurrentThreadId().ToString() + " @ :" + DateTime.Now.ToString() + " #%#%#%#%#%");
strBuild.AppendLine(ex.Message);
string[] ribals = ex.StackTrace.Split('\n');
strBuild.AppendLine(String.Join("\n", ribals.Take(3).ToArray()));
strBuild.AppendLine("\nLike this you can have many more lines ...\n");
strBuild.AppendLine("Main issue found here :\n" + ribals.Last());
strBuild.AppendLine("#%#%#%#%#% We Ran out of Stack Space on thread id : " + GetCurrentThreadId().ToString() + " @ :" + DateTime.Now.ToString() + " #%#%#%#%#%");
return strBuild.ToString();
}
Exception inner = ex;
Enumerable.Range(0, 30).All(x =>
{
if (x == 0) strBuild.Append("########## Exception begin on thread id : " + GetCurrentThreadId().ToString() + " @ :" + DateTime.Now.ToString() + " ##########\n");
strBuild.Append("---------------------[" + x.ToString() + "]---------------------\n");
strBuild.Append("Message : " + inner.Message + "\nStack Trace : " + inner.StackTrace + "\n");
strBuild.Append("---------------------[" + x.ToString() + "]---------------------\n");
inner = inner.InnerException;
if (inner == null)
{
strBuild.Append("########## Exception End on thread id : " + GetCurrentThreadId().ToString() + " @ :" + DateTime.Now.ToString() + " ##########\n\n");
return false;
}
return true;
});
return strBuild.ToString();
}
}