Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 捕获异常的推荐方法是什么_C#_Exception - Fatal编程技术网

C# 捕获异常的推荐方法是什么

C# 捕获异常的推荐方法是什么,c#,exception,C#,Exception,我必须做一个代码审查,我得到了一个代码部分,解决可能的异常。在我看来,开发人员编码是可行的,但我想问一下,通常的正确方法是什么。捕获异常的最佳方法是什么? 编码员写道: try { . . . } catch (Exception ex) { if (ex is PlatformNotSupportedException) { //for the Windows version or edition that does not support. // t

我必须做一个代码审查,我得到了一个代码部分,解决可能的异常。在我看来,开发人员编码是可行的,但我想问一下,通常的正确方法是什么。捕获异常的最佳方法是什么? 编码员写道:

  try 
  { . . . }
  catch (Exception ex)
  {
    if (ex is PlatformNotSupportedException)
    { //for the Windows version or edition that does not support.
    // tracing
    }
    else if (ex is NotSupportedException || ex is IOException)
    { // for the NTFS not supported or EFS is not configured
      // tracing
    }
    else
    {
      //report any exception as encrypt/decrypt
    }
  }
我以为书上说应该是:

  catch (PlatformNotSupportedException pnse)
  {
    //for the Windows version or edition that does not support.
    // tracing
  }
  catch (NotSupportedException nse)
  {
    // for the NTFS not supported or EFS is not configured
    // tracing
  }
  catch (IOException ioe)
  {
    // tracing for IOE
  }   
  catch (Exception e)
  {
      //report any exception as encrypt/decrypt
  }

第二种方法更可取。然而,在提出的解决方案和当前的解决方案之间存在微小的差异。您需要重构到一个方法,或者在NotSupportedException和IOException catch块的两个位置复制代码,而当前实现在同一个if块下处理它

因此,如果您想遵循相同的方法,可以使用when来过滤某些类型和更多类型

  catch (PlatformNotSupportedException pnse)
  {
    // for the Windows version or edition that does not support.
    // tracing
  }
  catch (Exception ex) when (ex is NotSupportedException || ex is IOException)
  {
    // for the NTFS not supported or EFS is not configured
    // tracing
  } 
  catch (Exception e)
  {
      //report any exception as encrypt/decrypt
  }

如果这不是强制性的,您可以让实现保持原样

第二种方法更可取。然而,在提出的解决方案和当前的解决方案之间存在微小的差异。您需要重构到一个方法,或者在NotSupportedException和IOException catch块的两个位置复制代码,而当前实现在同一个if块下处理它

因此,如果您想遵循相同的方法,可以使用when来过滤某些类型和更多类型

  catch (PlatformNotSupportedException pnse)
  {
    // for the Windows version or edition that does not support.
    // tracing
  }
  catch (Exception ex) when (ex is NotSupportedException || ex is IOException)
  {
    // for the NTFS not supported or EFS is not configured
    // tracing
  } 
  catch (Exception e)
  {
      //report any exception as encrypt/decrypt
  }

如果这不是强制性的,您可以让实现保持原样,这对于所有类型的异常都是通用的

try 
{
   .....code
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

这对于所有类型的异常都是通用的

try 
{
   .....code
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
TLDR:使用第二种形式,以便编译器捕获排序错误

您应该使用第二种形式的原因是,如果您试图以错误的顺序处理类型,那么您将得到一个编译错误

例如,这将给您一个实际的编译器错误:

try
{
    throw new ArgumentOutOfRangeException();
}

catch (Exception)
{
    Console.WriteLine("Caught 'Exception'");
}

// This gives a compile error:
// "Error CS0160  A previous catch clause already catches all exceptions of this or of a super type ('Exception')"

catch (SystemException)
{
    Console.WriteLine("Caught 'SystemException'");
}
但是,使用if/else if不会导致编译错误,因此不会注意到该错误:

try
{
    throw new ArgumentOutOfRangeException();
}

catch (Exception ex)
{
    if (ex is Exception)
    {
        Console.WriteLine("Caught 'Exception'");
    }
    else if (ex is SystemException) // This will never be reached, but no compile error.
    {
        Console.WriteLine("Caught 'SystemException'");
    }
}
但是,请注意,诸如Resharper之类的工具将针对第二种情况向您发出警告。

TLDR:使用第二种形式,以便编译器捕获排序错误

您应该使用第二种形式的原因是,如果您试图以错误的顺序处理类型,那么您将得到一个编译错误

例如,这将给您一个实际的编译器错误:

try
{
    throw new ArgumentOutOfRangeException();
}

catch (Exception)
{
    Console.WriteLine("Caught 'Exception'");
}

// This gives a compile error:
// "Error CS0160  A previous catch clause already catches all exceptions of this or of a super type ('Exception')"

catch (SystemException)
{
    Console.WriteLine("Caught 'SystemException'");
}
但是,使用if/else if不会导致编译错误,因此不会注意到该错误:

try
{
    throw new ArgumentOutOfRangeException();
}

catch (Exception ex)
{
    if (ex is Exception)
    {
        Console.WriteLine("Caught 'Exception'");
    }
    else if (ex is SystemException) // This will never be reached, but no compile error.
    {
        Console.WriteLine("Caught 'SystemException'");
    }
}

但是,请注意,对于第二种情况,诸如Resharper之类的工具将向您发出警告。

您是正确的,尽管我知道它们来自OR。当e不受支持时,该或可能被重构为捕获异常e。异常| | e是IOException,然后使用第二个选项。@John这本书可能在添加异常之前就开始了filters@Marc事实上,如果我没记错的话,我认为它是在C6中引入的。我更想说明为什么编写顶级代码的开发人员可能会这样做。你是对的,尽管我知道它们来自OR。当e不受支持时,该或可能被重构为捕获异常e。异常| | e是IOException,然后使用第二个选项。@John这本书可能在添加异常之前就开始了filters@Marc事实上,如果我没记错的话,我认为它是在C6中引入的。我更想说明为什么编写顶级代码的开发人员可能会这样做。我自己也在努力解决这个问题,但我更喜欢你的解释-我喜欢这个答案,因为它并不完全是紧张的。最终,它会尽可能清晰地表达逻辑,并证明是愚蠢的。但为什么第二种方法会更可取呢?目前,这个答案只是表达了一种观点。我的意思是,我确实同意,但这个答案需要证明是正确的。我自己也在努力寻找答案,但我更喜欢你的解释-我喜欢这个答案,因为它并不完全是紧张的。最终,它会尽可能清晰地表达逻辑,并证明是愚蠢的。但为什么第二种方法会更可取呢?目前,这个答案只是表达了一种观点。我的意思是,我确实同意,但答案需要证明。在这种情况下,似乎有特殊的事情要做,所以…在这种情况下,似乎有特殊的事情要做,所以。。。