本文介绍了C#异常处理通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在C#中轻松实现以下pseduo代码:

Is there any way in C# to easily achieve the following pseduo-code:

try
{
...
}
catch ( ExceptionTypeA, ExceptionTypeB, ExceptionTypeC as ex)
{
... same code for all threw
}

try
{
...
}
catch ( ExceptionTypeA ex )
catch ( ExceptionTypeB ex )
catch ( ExceptionTypeC ex )
{
... same code for all exceptions of A, B or C
}

的所有例外的相同代码我想我是什么对于异常类型,m表示将会很好。

I guess what I'm saying would be great would be fall-through on exception types.

推荐答案

提到的语法问题(带有 ex )是:什么属性/ membe rs应该 ex 有?不同的异常类型不一定兼容,除非有涉及的继承(在这种情况下,捕获您最关心的最少派生)。

The problem with the syntax as mentioned (with an ex) is: what properties/members should ex have? Different exception types are not necessarily compatible, unless there is inheritance involved (in which case, catch the least-derived that you care about).

唯一的当前选项是只需使用异常ex (或类似的)和检查( / as )在处理程序内。

The only current option is to just use Exception ex (or similar) and check (is/as) inside the handler.

或;将共同代码重构成一个可以被三方使用的方法?

Or; refactor the common code into a method that can be used by all three?

这篇关于C#异常处理通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 10:01