Contents

Clemens Vasters - Are you catching falling knives?里给了一个判断C#的exception是不是fatal的代码,可以参考参考。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static bool IsFatal(this Exception exception)
{
while (exception != null)
{
if (exception as OutOfMemoryException != null && exception as InsufficientMemoryException == null || exception as ThreadAbortException != null ||
exception as AccessViolationException != null || exception as SEHException != null || exception as StackOverflowException != null)
{
return true;
}
else
{
if (exception as TypeInitializationException == null && exception as TargetInvocationException == null)
{
break;
}
exception = exception.InnerException;
}
}
return false;
}
Contents