在净抛出多个异常/ C#多个、抛出、异常

2023-09-03 00:01:52 作者:猫九命

在我的工作中的应用程序,任何业务逻辑错误导致一个异常被抛出,并且调用code处理该异常。这种模式是用于整个应用程序和行之有效的。

In an application I work on, any business logic error causes an exception to be thrown, and the calling code handles the exception. This pattern is used throughout the application and works well.

我有一个情况我会尝试从业务层内部执行了一些业务任务。对此的要求是,一个任务失败不应导致进程终止。其他的任务应该还是能够执行。换句话说,这不是一个原子操作。我的问题是,在手术结束时,我谨通知调用code,一个异常或异常确实发生抛出一个异常。请看下面的psuedo- code片断:

I have a situation where I will be attempting to execute a number of business tasks from inside the business layer. The requirement for this is that a failure of one task should not cause the process to terminate. Other tasks should still be able to execute. In other words, this is not an atomic operation. The problem I have is that at the end of the operation, I wish to notify the calling code that an exception or exceptions did occur by throwing an exception. Consider the following psuedo-code snippet:

function DoTasks(MyTask[] taskList)
{
  foreach(MyTask task in taskList)
  {
    try
    {
       DoTask(task);
    }
    catch(Exception ex)
    {
        log.add(ex);
    }
  }

  //I want to throw something here if any exception occurred
}

我怎么扔?我在我的职业生涯遇到过这种模式。过去我一直都异常的列表,然后抛出一个包含所有捕获的异常异常。这似乎不是最优雅的方式。其重要preserve尽可能多的细节尽可能从各个例外present给调用code。

What do I throw? I have encountered this pattern before in my career. In the past I have kept a list of all exceptions, then thrown an exception that contains all the caught exceptions. This doesn't seem like the most elegant approach. Its important to preserve as many details as possible from each exception to present to the calling code.

思考?

编辑:该解决方案必须写在.NET 3.5中。所提到的布拉德利固安捷(下)我不能使用任何测试库,或AggregateException在.NET 4.0中会是一个很好的解决方案,收集异常抛出。

The solution must be written in .Net 3.5. I cannot use any beta libraries, or the AggregateException in .Net 4.0 as mentioned by Bradley Grainger (below) would be a nice solution for collection exceptions to throw.

推荐答案

在任务并行库扩展了解。 NET(其中将成为.NET 4.0 部分)按照建议的模式其他答案:收集已经被扔到一个AggregateException类的所有异常

The Task Parallel Library extensions for .NET (which will become part of .NET 4.0) follow the pattern suggested in other answers: collecting all exceptions that have been thrown into an AggregateException class.

通过总是投掷相同类型(是否存在来自子工作一个例外,或许多)时,调用code表示处理该异常更容易编写。

By always throwing the same type (whether there is one exception from the child work, or many), the calling code that handles the exception is easier to write.

在.NET 4.0 CTP, AggregateException 有一个公共的构造函数(即取的IEnumerable<异常> );它可能是您的应用程序的理想选择。

In the .NET 4.0 CTP, AggregateException has a public constructor (that takes IEnumerable<Exception>); it may be a good choice for your application.

如果你的目标.NET 3.5,考虑克隆 System.Threading.AggregateException 类的,你需要在自己的code部位,例如,一些构造和InnerExceptions财产。 (你可以把在的System.Threading 命名空间的组件内,这可能会导致混乱,如果你公开暴露它的克隆,但将升级到4.0更容易稍后。)当.NET 4.0发布之后,你应该能够通过删除包含从项目的克隆源文件,改变项目为目标的新框架版本,和重建的升级为框架式。当然,如果你这样做,你需要在Microsoft发布新的CTP版本,让您的code不会成为不相容仔细跟踪更改此类。 (例如,这似乎是一个有用的通用类,他们可以从的System.Threading 将它移动到系统)。在最坏的情况下,你可以重命名的类型和移动回你自己的命名空间(这是很容易与大多数重构工具)。

If you're targeting .NET 3.5, consider cloning the parts of the System.Threading.AggregateException class that you need in your own code, e.g., some of the constructors and the InnerExceptions property. (You can place your clone in the System.Threading namespace inside your assembly, which could cause confusion if you exposed it publicly, but will make upgrading to 4.0 easier later on.) When .NET 4.0 is released, you should be able to "upgrade" to the Framework type by deleting the source file containing your clone from your project, changing the project to target the new framework version, and rebuilding. Of course, if you do this, you need to carefully track changes to this class as Microsoft releases new CTPs, so that your code doesn't become incompatible. (For example, this seems like a useful general-purpose class, and they could move it from System.Threading to System.) In the worst case, you can just rename the type and move it back into your own namespace (this is very easy with most refactoring tools).

 
精彩推荐
图片推荐