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)
试试这个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)
| 归档时间: |
|
| 查看次数: |
63 次 |
| 最近记录: |