如何过滤掉catch块

isx*_*ker 2 .net c# exception try-catch

return false如果SP.ServerException被抛出,我需要防止记录.但在所有其他情况下,我也需要进行日志记录return false.

try
{
    folder = GetFolderByRelativeUrl(folderRelativePath);
}
catch (SP.ServerException serverEx)
{
    //if file is not found the logging is not need
    if (serverEx?.Message == "File not found")
    {
        return false;
    }
    //how i can go from here
}
catch (Exception ex)
{
    //to there
    Log(ex.Message);
    return false;
}
Run Code Online (Sandbox Code Playgroud)

我知道解决方案可能是

try
{
    folder = GetFolderByRelativeUrl(folderRelativePath);
}
catch (Exception ex)
{
    //if file is not found the logging is not need
    if (!(ex is SP.ServerException && ex?.Message == "File not found"))
    {
        Log(ex.Message);
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

Wal*_*mar 6

试试这个when关键字:

try
{
    folder = GetFolderByRelativeUrl(folderRelativePath);
}
catch (SP.ServerException serverEx) when (serverEx.Message == "File not found")
{
   return false;
}
catch (Exception ex)
{
    //to there
    Log(ex.Message);
    return false;
}
Run Code Online (Sandbox Code Playgroud)