异常处理语句 您所在的位置:网站首页 catch的单词怎么读音 异常处理语句

异常处理语句

2023-12-18 03:27| 来源: 网络整理| 查看: 265

异常处理语句 - throw、try-catch、try-finally 和 try-catch-finally 项目 06/05/2023

使用 throw 和 try 语句来处理异常。 使用 throw 语句引发异常。 使用 try 语句捕获和处理在执行代码块期间可能发生的异常。

throw 语句

throw 语句引发异常:

if (shapeAmount = 1 ? args[0] : throw new ArgumentException("Please supply at least one argument.");

null 合并运算符。 以下示例使用 throw 表达式在要分配给属性的字符串为 null 时引发 ArgumentNullException:

public string Name { get => name; set => name = value ?? throw new ArgumentNullException(paramName: nameof(value), message: "Name cannot be null"); }

expression-bodied lambda 或方法。 以下示例使用 throw 表达式引发 InvalidCastException,以指示不支持转换为 DateTime 值:

DateTime ToDateTime(IFormatProvider provider) => throw new InvalidCastException("Conversion to a DateTime is not supported."); try 语句

可以通过以下任何形式使用 try 语句:try-catch - 处理在 try 块内执行代码期间可能发生的异常,try-finally - 指定在控件离开 try 块时执行的代码,以及 try-catch-finally - 作为上述两种形式的组合。

try-catch 语句

使用 try-catch 语句处理在执行代码块期间可能发生的异常。 将代码置于 try 块中可能发生异常的位置。 使用 catch 子句指定要在相应的 catch 块中处理的异常的基类型:

try { var result = Process(-3, 4); Console.WriteLine($"Processing succeeded: {result}"); } catch (ArgumentException e) { Console.WriteLine($"Processing failed: {e.Message}"); }

可以提供多个 catch 子句:

try { var result = await ProcessAsync(-3, 4, cancellationToken); Console.WriteLine($"Processing succeeded: {result}"); } catch (ArgumentException e) { Console.WriteLine($"Processing failed: {e.Message}"); } catch (OperationCanceledException) { Console.WriteLine("Processing is cancelled."); }

发生异常时,将从上到下按指定顺序检查 catch 子句。 对于任何引发的异常,最多只执行一个 catch 块。 如前面的示例所示,可以省略异常变量的声明,并在 catch 子句中仅指定异常类型。 没有任何指定异常类型的 catch 子句与任何异常匹配,如果存在,则必须是最后一个 catch 子句。

如果要重新引发捕获的异常,请使用 throw 语句,如以下示例所示:

try { var result = Process(-3, 4); Console.WriteLine($"Processing succeeded: {result}"); } catch (Exception e) { LogError(e, "Processing failed."); throw; }

注意

throw; 保留异常的原始堆栈跟踪,该跟踪存储在 Exception.StackTrace 属性中。 与此相反,throw e; 更新 e 的 StackTrace 属性。

when 异常筛选器

除了异常类型之外,还可以指定异常筛选器,该筛选器进一步检查异常并确定相应的 catch 块是否处理该异常。 异常筛选器是遵循 when 关键字的布尔表达式,如以下示例所示:

try { var result = Process(-3, 4); Console.WriteLine($"Processing succeeded: {result}"); } catch (Exception e) when (e is ArgumentException || e is DivideByZeroException) { Console.WriteLine($"Processing failed: {e.Message}"); }

前面的示例使用异常筛选器提供单个 catch 块来处理两个指定类型的异常。

可以为相同异常类型提供若干 catch 子句,如果它们通过异常筛选器区分。 其中一个子句可能没有异常筛选器。 如果存在此类子句,则它必须是指定该异常类型的最后一个子句。

如果 catch 子句具有异常筛选器,则可以指定与 catch 子句之后出现的异常类型相同或小于派生的异常类型。 例如,如果存在异常筛选器,则 catch (Exception e) 子句不需要是最后一个子句。

异步和迭代器方法中的异常

如果异步函数中发生异常,则等待函数的结果时,它会传播到函数的调用方,如以下示例所示:

public static async Task Run() { try { Task processing = ProcessAsync(-1); Console.WriteLine("Launched processing."); int result = await processing; Console.WriteLine($"Result: {result}."); } catch (ArgumentException e) { Console.WriteLine($"Processing failed: {e.Message}"); } // Output: // Launched processing. // Processing failed: Input must be non-negative. (Parameter 'input') } private static async Task ProcessAsync(int input) { if (input < 0) { throw new ArgumentOutOfRangeException(nameof(input), "Input must be non-negative."); } await Task.Delay(500); return input; }

如果迭代器方法中发生异常,则仅当迭代器前进到下一个元素时,它才会传播到调用方。

try-finally 语句

在 try-finally 语句中,当控件离开 try 块时,将执行 finally 块。 控件可能会离开 try 块,因为

正常执行, 执行 jump 语句(即 return、break、continue 或 goto),或 从 try 块中传播异常。

以下示例使用 finally 块在控件离开方法之前重置对象的状态:

public async Task HandleRequest(int itemId, CancellationToken ct) { Busy = true; try { await ProcessAsync(itemId, ct); } finally { Busy = false; } }

还可以使用 finally 块来清理 try 块中使用的已分配资源。

注意

当资源类型实现 IDisposable 或 IAsyncDisposable 接口时,请考虑 using 语句。 using 语句可确保在控件离开 using 语句时释放获取的资源。 编译器将 using 语句转换为 try-finally 语句。

在几乎所有情况下,都会执行 finally 块。 未执行 finally 块的唯一情况涉及立即终止程序。 例如,由于 Environment.FailFast 调用或 OverflowException 或 InvalidProgramException 异常,可能会发生此类终止。 大多数操作系统在停止和卸载进程的过程中执行合理的资源清理。

try-catch-finally 语句

使用 try-catch-finally 语句来处理在执行 try 块期间可能发生的异常,并指定在控件离开 try 语句时必须执行的代码:

public async Task ProcessRequest(int itemId, CancellationToken ct) { Busy = true; try { await ProcessAsync(itemId, ct); } catch (Exception e) when (e is not OperationCanceledException) { LogError(e, $"Failed to process request for item ID {itemId}."); throw; } finally { Busy = false; } }

当 catch 块处理异常时,finally 块在执行该 catch 块后执行(即使执行 catch 块期间发生另一个异常)。 有关 catch 和 finally 块的信息,请分别参阅 try-catch 语句和 try-finally 语句 部分。

C# 语言规范

有关更多信息,请参阅 C# 语言规范的以下部分:

throw 语句 try 语句 异常 另请参阅 C# 参考 异常和异常处理 在 .NET 中处理和引发异常 引发首选项(样式规则 IDE0016) AppDomain.FirstChanceException AppDomain.UnhandledException TaskScheduler.UnobservedTaskException


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有