C# 试着抓。以相同的方式处理多个异常(或失败)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2291397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:08:03  来源:igfitidea点击:

try- catch. Handling multiple exceptions the same way (or with a fall through)

c#exceptionexception-handlingerror-handling

提问by ram

There has already been a question posted herewhich is very similar. Mine is extending that question a bit more. Say you want to catch multiple types of exception but want to handle it the same way, is there a way to do something like switch case ?

这里已经发布一个非常相似的问题。我正在进一步扩展这个问题。假设您想捕获多种类型的异常但想以相同的方式处理它,有没有办法做类似 switch case 的事情?

switch (case)
{
  case 1:
  case 2:

  DoSomething();
  break;
  case 3:
  DoSomethingElse()
  break;

}

Is it possible to handle few exceptions the same way . Something like

是否有可能以同样的方式处理少数异常。就像是

try
{
}
catch (CustomException ce)
catch (AnotherCustomException ce)
{
  //basically do the same thing for these 2 kinds of exception
  LogException();
}
catch (SomeOtherException ex)
{
 //Do Something else
}

采纳答案by Jo?o Angelo

Currently there is no language construct to accomplish what you want. Unless the exception all derive from a base exception you need to consider refactoring the common logic to a method and call it from the different exception handlers.

目前没有语言结构来完成你想要的。除非异常都源自基本异常,否则您需要考虑将公共逻辑重构为方法并从不同的异常处理程序调用它。

Alternatively you could do as explained in this question:

或者,您可以按照此问题中的说明进行操作:

Catch multiple Exceptions at once?

一次捕获多个异常?

Personally I tend to prefer the method-based approach.

我个人倾向于更喜欢基于方法的方法。

回答by pdr

You should really have a BaseCustomException and catch that.

你真的应该有一个 BaseCustomException 并抓住它。

回答by Stan R.

You shouldn't be catching this many custom exceptions,however if you want you can create a common BaseExceptionand catch that.

您不应该捕获这么多自定义异常,但是如果您愿意,您可以创建一个通用异常BaseException并捕获它。

回答by torak

I've never actually done this or anything like it, and I don't have access to a compiler for testing purposes but surely something like this would work. Not sure how to actually do the type comparison or if C# would let you replace the if statements with a case statement.

我从来没有真正做过这个或类似的事情,而且我无法访问用于测试目的的编译器,但肯定像这样的东西会起作用。不确定如何实际进行类型比较,或者 C# 是否允许您用 case 语句替换 if 语句。

try 
{ 
}
catch (System.Object obj)
{
  Type type;

  type = obj.GetType() ;
  if (type == CustomException || type == AnotherCustomException)
  { 
    //basically do the same thing for these 2 kinds of exception 
    LogException(); 
  } 
  else if  (type == SomeOtherException ex) 
  { 
    //Do Something else 
  }
  else
  {
    // Wasn't an exception to handle here
    throw obj;
  }
}

回答by supercat

In vb.net, one can use exception filters to say, e.g.

在 vb.net 中,可以使用异常过滤器来表示,例如

  Catch Ex As Exception When TypeOf Ex is ThisException Or TypeOf Ex is ThatException

Unfortunately, for whatever reasons, the implementors of C# have as yet refused to allow exception filtering code to be written within C#.

不幸的是,无论出于何种原因,C# 的实现者都拒绝允许在 C# 中编写异常过滤代码。

回答by Jacob Brewer

This is copied from another posting, but I am pulling the code to this thread:

这是从另一个帖子复制的,但我将代码拉到这个线程:

Catch System.Exceptionand switch on the types

捕捉System.Exception并开启类型

catch (Exception ex)            
{                
    if (ex is FormatException || ex is OverflowException)
    {
        WebId = Guid.Empty;
        return;
    }

    throw;
}

I prefer this to repeating a method call in several catch blocks.

我更喜欢在几个 catch 块中重复一个方法调用。