取消异常相关

This commit is contained in:
龙腾猫跃 committed 2026-06-23 08:33:17 +08:00
1 parent 20ed81ed9e
commit d66af3d6cd
3 files changed
+22 -4

No files matched your search

+18
View File
@@ -11,6 +11,24 @@ public static class ExceptionExtensions {
return ex;
}
/// <summary>
/// 该异常或其子异常是否为 <see cref="OperationCanceledException"/>、<see cref="TaskCanceledException"/> 或 <see cref="ThreadInterruptedException"/>。
/// </summary>
public static bool IsCanceled(this Exception ex) {
foreach (var inner in ex.Flatten()) {
if (inner is OperationCanceledException or TaskCanceledException or ThreadInterruptedException) return true;
}
return false;
}
/// <summary>
/// 当该异常或其子异常是 <see cref="OperationCanceledException"/>、<see cref="TaskCanceledException"/> 或 <see cref="ThreadInterruptedException"/> 时,抛出对应异常。
/// </summary>
public static void ThrowOnCanceled(this Exception ex) {
foreach (var inner in ex.Flatten()) {
if (inner is OperationCanceledException or TaskCanceledException or ThreadInterruptedException) throw inner;
}
}
/// <summary>
/// 将异常所有的 <see cref="Exception.InnerException"/> 展开。
/// 若为 <see cref="AggregateException"/>,也会展开其中所有的子异常。