Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Exception 处理SharePoint 2010项目中异常的正确方法_Exception_Sharepoint_Exception Handling_Sharepoint 2010 - Fatal编程技术网

Exception 处理SharePoint 2010项目中异常的正确方法

Exception 处理SharePoint 2010项目中异常的正确方法,exception,sharepoint,exception-handling,sharepoint-2010,Exception,Sharepoint,Exception Handling,Sharepoint 2010,我是sharepoint开发的新手,需要您的帮助,告诉我如何处理代码中的潜在异常。我有一个以用户用户为参数的方法,在这个方法中,我检测谁是当前用户,所以我可以根据他的名字从SharePoint列表中进行一些查询并返回信息。我需要处理哪些可能的例外情况,以及这样做的“良好”实践是什么 谢谢你的时间和回答 以下是我迄今为止编写的代码: public void SomeMethod(User user) { if (user == null) { throw new A

我是sharepoint开发的新手,需要您的帮助,告诉我如何处理代码中的潜在异常。我有一个以
用户用户
为参数的方法,在这个方法中,我检测谁是当前用户,所以我可以根据他的名字从
SharePoint列表
中进行一些查询并返回信息。我需要处理哪些可能的例外情况,以及这样做的“良好”实践是什么

谢谢你的时间和回答

以下是我迄今为止编写的代码:

public void SomeMethod(User user)
{
    if (user == null)
    {
        throw new ArgumentNullException("Employee object is not created");
    }

    try
    {
        using (SPSite currentSite = new SPSite("SiteName"))
        {
            if (currentSite == null)
            {
                throw new System.UriFormatException("Invalid URL");
            }
            using (SPWeb currentWeb = currentSite.OpenWeb())
            {
                if (currentWeb.CurrentUser == null)
                {
                    throw new Exception("User is not logged in");
                }
                user.Name = currentWeb.CurrentUser.Name;

                if (currentWeb.Lists["ListName"] == null)
                {
                    throw new Exception("There is no list with that name");
                }
                SPList myList = currentWeb.Lists["ListName"];
                SPQuery queryRole = new SPQuery();

                queryRole.Query = "SomeQuery";


                    }
                }
            }
        }
    }
    catch (UriFormatException ex)
    {
        throw new UriFormatException(ex.Message);
    }
    catch (ArgumentNullException ex)
    {
        throw new ArgumentNullException(ex.Message);
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }

我想你为了隐私的目的删除了一些东西,因为我不知道你想达到什么目的。 如果根据此方法运行的上下文(Webpart/.aspx/etc)需要当前用户,则可以使用SPContext.current.Web.CurrentUser

如果这是一个独立的桌面应用程序,你必须按照你已经在做的方式创建一个新的web,但这仍然不能解释“user.Name=…”行

至少要在某种程度上解决这个问题,创建一个异常只是为了自己捕获它,然后再次抛出它是毫无意义的

throw new UriFormatException("...");
...
catch(UriException ex)
{
    throw new UriFormatException(ex.Message);
}
一般的规则是,只有在需要并且能够处理异常时才捕获异常。没有一个catch块实际处理异常,因此您最好将它们全部删除

为了找出潜在的例外情况,msdn文档是一个很好的起点:

是关于sharepoint异常处理的问题