What benefit does the new “Exception filter” feature provide?

前端 未结 5 2256
暖寄归人
暖寄归人 2020-12-08 19:11

C# 6 has a new feature called \"exception filtering\"

The syntax is like this:

catch (Win32Exception exception) when (exception.NativeErrorCode == 0x         


        
5条回答
  •  一个人的身影
    2020-12-08 20:09

    The real benefit IMO:

    try
    {
    }
    catch (SomeException ex) if (ex.Something)
    {
    }
    catch (Exception ex)
    {
      // SomeException with !ex.Something is caught here
      // not the same as calling `throw` for !ex.Something 
      // without filters in previous handler
      // less code duplication
    }
    

提交回复
热议问题