Concurrency in CSharp Cookbook中文翻译:5.2传播错误

2024-01-05 04:04

本文主要是介绍Concurrency in CSharp Cookbook中文翻译:5.2传播错误,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Problem 问题

You need a way to respond to errors that can happen in your dataflow mesh.
您需要一种方法来响应数据流网格中可能发生的错误。

Solution 解决方法

If a delegate passed to a dataflow block throws an exception, then that block will enter a faulted state. When a block is in a faulted state, it will drop all of its data (and stop accepting new data). The block in the following code will never produce any output data; the first value raises an exception, and the second value is just dropped:
如果传递给数据流块的委托抛出异常,则该块将进入故障状态。当一个块处于故障状态时,它将丢弃所有数据(并停止接受新数据)。下面代码中的块永远不会产生任何输出数据;第一个值引发异常,第二个值被丢弃:
var block = new TransformBlock(item =>{ if (item == 1)throw new InvalidOperationException("Blech."); return item * 2; }); 
block.Post(1); 
block.Post(2);
To catch exceptions from a dataflow block, you should await its Completion property. The Completion property returns a Task that will complete when the block is completed, and if the block faults, the Completion task is also faulted:
要从数据流块捕获异常,应该等待它的Completion属性。Completion属性返回一个任务,该任务将在块完成时完成,如果块出现故障,则Completion任务也会出现故障:
try 
{ var block = new TransformBlock(item =>{ if (item == 1)throw new InvalidOperationException("Blech."); return item * 2; }); block.Post(1); await block.Completion; 
} 
catch (InvalidOperationException) 
{ // The exception is caught here. 
}
When you propagate completion using the PropagateCompletion link option,errors are also propagated. However, the exception is passed to the next block wrapped in an AggregateException. The following example catches the exception from the end of a pipeline, so it would catch AggregateException if an exception was propagated from earlier blocks:
当您使用PropagateCompletion链接选项传播完成时,也会传播错误。然而,异常被传递到封装在AggregateException中的下一个块。下面的例子从管道的末端捕获异常,所以如果一个异常是从早期块传播的,它将捕获AggregateException:
try
{var multiplyBlock = new TransformBlock<int, int>(item =>{if (item == 1)throw new InvalidOperationException("Blech.");return item * 2;});var subtractBlock = new TransformBlock<int, int>(item => item - 2);multiplyBlock.LinkTo(subtractBlock,new DataflowLinkOptions { PropagateCompletion = true });multiplyBlock.Post(1);await subtractBlock.Completion;
}
catch (AggregateException)
{// The exception is caught here.
}
When you propagate completion using the PropagateCompletion link option,errors are also propagated. However, the exception is passed to the next block wrapped in an AggregateException. Each block wraps incoming errors in an AggregateException, even if the incoming error is already an AggregateException. If an error occurs early in a pipeline and travels down several links before it’s observed, the original error will be wrapped in multiple layers of AggregateException. The AggregateException.Flatten method simplifies error handling in this scenario.The following example catches the exception from the end of a pipeline, so it would catch AggregateException if an exception was propagated from earlier blocks:
当您使用PropagateCompletion链接选项传播完成时,也会传播错误。然而,异常被传递到封装在AggregateException中的下一个块。每个块将传入的错误包装在AggregateException中,即使传入的错误已经是AggregateException。如果错误发生在管道的早期,并且在观察到它之前沿着几个链接传播,那么原始错误将被包装在多层AggregateException中。AggregateException。Flatten方法简化了这种情况下的错误处理。下面的例子从管道的末端捕获异常,所以如果一个异常是从早期块传播的,它将捕获AggregateException:

Discussion 讨论

When you build your mesh (or pipeline), consider how errors should be handled. In simpler situations, it can be best to just propagate the errors and catch them once at the end. In more complex meshes, you may need to observe each block when the dataflow has completed.
当您构建网格(或管道)时,请考虑如何处理错误。在更简单的情况下,最好只传播错误并在最后捕获一次。在更复杂的网格中,您可能需要在数据流完成时观察每个块。
Alternatively, if you want your blocks to remain viable in the face of exceptions, you can choose to treat exceptions as another kind of data and let them flow through your mesh along with your correctly processed data items. Using that pattern, you can keep your dataflow mesh operational, since the blocks themselves don’t fault and continue processing the next data item. See Recipe 14.6 for more details.
或者,如果您希望您的块在面对异常时仍然可行,您可以选择将异常视为另一种数据,并让它们与正确处理的数据项一起流经网格。使用该模式,您可以保持数据流网格的可操作性,因为块本身不会出错并继续处理下一个数据项。参见Recipe 14.6了解更多细节。

这篇关于Concurrency in CSharp Cookbook中文翻译:5.2传播错误的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/571596

相关文章

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

Python中ModuleNotFoundError: No module named ‘timm’的错误解决

《Python中ModuleNotFoundError:Nomodulenamed‘timm’的错误解决》本文主要介绍了Python中ModuleNotFoundError:Nomodulen... 目录一、引言二、错误原因分析三、解决办法1.安装timm模块2. 检查python环境3. 解决安装路径问题

如何解决mysql出现Incorrect string value for column ‘表项‘ at row 1错误问题

《如何解决mysql出现Incorrectstringvalueforcolumn‘表项‘atrow1错误问题》:本文主要介绍如何解决mysql出现Incorrectstringv... 目录mysql出现Incorrect string value for column ‘表项‘ at row 1错误报错

SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法

《SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法》本文主要介绍了SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法,具有一定的参考价值,感兴趣的可以了解一下... 目录方法1:更改IDE配置方法2:在Eclipse中清理项目方法3:使用Maven命令行在开发Sprin

前端bug调试的方法技巧及常见错误

《前端bug调试的方法技巧及常见错误》:本文主要介绍编程中常见的报错和Bug,以及调试的重要性,调试的基本流程是通过缩小范围来定位问题,并给出了推测法、删除代码法、console调试和debugg... 目录调试基本流程调试方法排查bug的两大技巧如何看控制台报错前端常见错误取值调用报错资源引入错误解析错误

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤

在MySQL执行UPDATE语句时遇到的错误1175的解决方案

《在MySQL执行UPDATE语句时遇到的错误1175的解决方案》MySQL安全更新模式(SafeUpdateMode)限制了UPDATE和DELETE操作,要求使用WHERE子句时必须基于主键或索引... mysql 中遇到的 Error Code: 1175 是由于启用了 安全更新模式(Safe Upd

SpringBoot中的404错误:原因、影响及解决策略

《SpringBoot中的404错误:原因、影响及解决策略》本文详细介绍了SpringBoot中404错误的出现原因、影响以及处理策略,404错误常见于URL路径错误、控制器配置问题、静态资源配置错误... 目录Spring Boot中的404错误:原因、影响及处理策略404错误的出现原因1. URL路径错

Spring常见错误之Web嵌套对象校验失效解决办法

《Spring常见错误之Web嵌套对象校验失效解决办法》:本文主要介绍Spring常见错误之Web嵌套对象校验失效解决的相关资料,通过在Phone对象上添加@Valid注解,问题得以解决,需要的朋... 目录问题复现案例解析问题修正总结  问题复现当开发一个学籍管理系统时,我们会提供了一个 API 接口去