C# equivalent to VB.NET's Catch…When

后端 未结 3 1287
栀梦
栀梦 2021-01-04 04:40

In VB.NET I often Catch…When:

Try
    …
Catch e As ArgumentNullException When e.ParamName.ToUpper() = \"SAMPLES\"
    …
End Try
<
3条回答
  •  忘掉有多难
    2021-01-04 05:02

    There's no equivalent to Catch…When in C#. You will really have to resort to an if statement inside your catch, then rethrow if your condition isn't fulfilled:

    try
    {
        …
    }
    catch (ArgumentNullException e)
    {
        if ("SAMPLES" == e.ParamName.ToUpper())
        {
            … // handle exception
        }
        else
        {
            throw;  // condition not fulfilled, let someone else handle the exception
        } 
    }
    

提交回复
热议问题