3
public SPListItemCollection GetACollection()
{
    try
    {
        //Method to get an item collection from somewhere
        if(itemCol != null)
            return itemCol;
        else
            return null;
    }
    catch(Exception ex)
    {
        LogException(ex);
        return null;
    }
}

此方法必须返回 null(不是空列表)——无论是否捕获到异常。上述工作 - 但有没有更好的方法呢?

4

11 回答 11

4
if(itemCol != null)
    return itemCol;
else
    return null;

简化为

return itemCol;

因为 if itemCol == nullthen 返回itemCol已经返回null。没有理由对这种行为进行特殊处理。

如果你想用 替换空集合null,你需要使用类似的东西:

if((itemCol != null) && itemCol.Any())
    return itemCol;
else
    return null;

一点设计建议:

  • 不鼓励吞咽异常。您应该只捕获一些您知道如何处理的特定异常
  • 空集合通常比null.
于 2012-06-01T14:35:57.463 回答
4

我必须首先说这是一件可怕的事情,吞下异常是可怕的!不要这样做!它会困扰你并使调试变得非常非常困难,更糟糕的是由于异常而返回 null - null 通常最终会抛出 NullReferenceException ,这将比被吞噬的异常更难调试(即使它已记录),但既然你问:

public SPListItemCollection GetACollection()
{
    SPListItemCollection itemCol = null;
    try
    {
        //Method to get an item collection from somewhere
    }
    catch(Exception ex)
    {
        LogException(ex);
    }
    return itemCol;
}
于 2012-06-01T14:36:32.563 回答
3

从不喜欢具有多出口点的方法,
在您的catch中将itemCol设置为 null,在try/catch返回 itemCol 之外

try
{
     // whatever
     if(itemCol.Count == 0) itemCol = null;
}
catch(Exception x)
{
     LogException(x);
     itemCol = null;
}
return itemCol;
于 2012-06-01T14:40:02.563 回答
2

您提到您当前的实施有效。我假设用于获取项目集合的方法返回包含 1 个或多个项目的集合或 null 或引发异常。如果这是真的,这将是一种选择。

public SPListItemCollection GetACollection()
{
   SPListItemCollection itemCol = null;
   try
   {
      itemCol = //Method to get an item collection from somewhere
   }
   catch(Exception e)
   {
      LogException(e);
   }
   return itemCol;
}
于 2012-06-01T15:19:50.430 回答
1

要检查集合是否为空,该Any()方法很有用,主要是如果您没有列表,但有一般的 IEnumerables。如果您只想摆脱 的重复return null;,您可以轻松地这样做:

public SPListItemCollection GetACollection()
{
    try
    {
        //Method to get an item collection from somewhere
        if(itemCol.Any())
            return itemCol;
    }
    catch(Exception ex)
    {
        LogException(ex);
    }

    return null;
}

您还可以附加一个

    finally
    {
        // Stuff that always needs to be done.
    }

直接在你的右括号之后catch

于 2012-06-01T14:38:28.570 回答
0

已经很好了,imo,我也会删除最后一个else,如下所示:

public SPListItemCollection GetACollection()
{
    try
    {
        //Method to get an item collection from somewhere
        if(itemCol != null)
            return itemCol;

        //Some if/else code here presumably... 

        //NO ELSE HERE...
        return null;
    }
    catch(Exception ex)
    {
        LogException(ex);
        return null;
    }
}
于 2012-06-01T14:35:39.443 回答
0

您不需要if-else阻止,因为如果 object 为 null,您已经返回 null:

public SPListItemCollection GetACollection()
{
    try
    {
        //Method to get an item collection from somewhere
        return itemCol;
    }
    catch(Exception ex)
    {
        LogException(ex);
        return null;
    }
}
于 2012-06-01T14:37:18.093 回答
0

这个怎么样?如果您的规格说明:

必须返回 null(不是空列表)

如果集合不为空,而是为空,您当前的实现将返回一个空集合。除非你依靠方法来处理它。

这照顾...

public SPListItemCollection GetACollection()
{
    try
    {
        //Method to get an item collection from somewhere
        if(itemCol != null && itemCol.Count == 0)
            return itemCol;

        return itemCol;
    }
    catch(Exception ex)
    {
        LogException(ex);
    }

    return null;
}
于 2012-06-01T14:39:19.397 回答
0

我会这样做:

public SPListItemCollection GetACollection()
{
    try
    {
        //Method to get an item collection from somewhere
        return itemCol != null && itemCol.Count == 0 ? null : itemCol;
    }
    catch(Exception ex)
    {
        LogException(ex);
        return null;
    }
}
于 2012-06-01T14:40:19.147 回答
0

你的代码看起来不错。如果它更符合您的口味,您可以将 null 返回放在最后:

public SPListItemCollection GetACollection()
{
    try
    {
        //Method to get an item collection from somewhere
        if(itemCol != null)
            return itemCol;
    }
    catch(Exception ex)
    {
        LogException(ex);
    }
    return null;
}
于 2012-06-01T14:43:38.097 回答
0

我同意以下观点:

来自 IDesign 编码标准 v2.4 ( http://idesign.net/Downloads/GetDownload/1985 ):

  1. 仅捕获您对其进行显式处理的异常。

让方法的调用者实现 try/catch 并处理异常。我会说在异常时返回 null 通常是一种不好的做法,因为您正在向调用者隐藏信息并阻止框架收集正确的调用堆栈。为什么首先抛出异常?同样,捕获您知道的显式异常类型,然后处理它。不要捕获其他/所有异常。

于 2019-12-10T14:07:32.767 回答