SharePoint中用不存在的"对象名"获取"对象"时的异常处理

文章主旨:分辨在SharePoint的WebApplication, Site, Web, List, Item, File级别下,用不存在的"对象名"获取"对象"时是否马上抛出异常。用形式化的方式表示如下:SPWebService.ContentService.WebApplications["webAppName"],webApp.Sites["siteName"],web.Webs["webName"],  web.Lists["listName"] and list.Items["itemIndex"], folder.Files["SeverRelativeUrl"]

经过自己的测试发现:webApplication, site, web, (即前三个)即使对象名字不存在也不会抛异常,而是在Try{}中继续执行后面语句;List, Item, File(即后三个)如果对象名字不存在,会获得不了该对象,直接抛异常去执行Catch(){}中的语句。

第一部分:测试webApplication, site, web级别。先定义每个级别中对象的数量都为0,然后用不存在的名字去获得对象之后,让其数量加1,如果输出的数量为0,则说明直接抛异常;如果输出的数量是1,则说明没有直接抛异常。请看如下代码:

1 using System;
2  using System.Collections.Generic;
3  using System.Linq;
4 using System.Text;
5 using Microsoft.SharePoint;
6 using Microsoft.SharePoint.Administration;
7
8 namespace MyTesting2
9 {
10 class Program
11 {
12 static void Main(string[] args)
13 {
14 //--SPWebApplication, SPSite and SPWeb don't throw exception.
15
16 SPWebApplicationCollection myWebAppCol = SPWebService.ContentService.WebApplications;
17 int webAppCount = 0;
18 try
19 {
20 //SharePoint - 123456的名字不存在
21 SPWebApplication testWebApp = myWebAppCol["SharePoint - 123456"];
22 webAppCount++;
23 Console.WriteLine(  "Program doesn't throw exception. And the count of webApp is: {0}", webAppCount);
24 }
25 catch
26 {
27 Console.WriteLine(  "Exception: The WebApplcition doesn't exist. And the count of WebApp is: {0}", webAppCount);
28 }
29
30
31 SPWebApplication mWebApp = myWebAppCol["SharePoint - 12345"];
32 int siteCount = 0;
33 try
34 {
35 //MyTeamSite100的名字不存在
36 SPSite testSite = mWebApp.Sites["MyTeamSite100"];
37 siteCount++;
38 Console.WriteLine("Program doesn't throw exception. And the count of site is: {0}", siteCount);
39 }
40 catch
41 {
42 Console.WriteLine(  "Exception: The site doesn't exist. And the count of site is: {0}", siteCount);
43 }
44
45
46 SPSite mySite = new SPSite("http://mosstemplate:12345/sites/MyTeamSiteCollection1");
47 SPWeb myRootWeb = mySite.RootWeb;
48 SPWeb myWeb = myRootWeb.Webs["MyTeamSite1"];
49
50 int webCount = 0;
51
52 try
53 {
54 //NewNewSubWeb的名字不存在
55 SPWeb testSubWeb = myWeb.Webs["NewNewSubWeb"];
56 webCount++;
57 Console.WriteLine("Program doesn't throw exception. And the count of web is: {0}", webCount);
58 }
59 catch
60 {
61 Console.WriteLine("Exception: This wed doesn't exist. And the count of web is: {0}", webCount);
62 }
63 }
64 }
65
66 }
67

经过我的测试,输出的结果如下:Program doesn't throw exception. And the count of webApp is: 1

                                          Program doesn't throw exception. And the count of site is: 1

                                          Program doesn't throw exception. And the count of web is: 1

可以看出,虽然用不存在的名字去获取相应的对象,程序也没有直接抛异常,而是继续执行后面的语句。

第二部分:测试List, Item, File级别。(判断方式和上面的一样)请看如下代码:

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.SharePoint;
6 using Microsoft.SharePoint.Administration;
7  
8
9 namespace MyTesting2
10 {
11 class Program
12 {
13 static void Main(string[] args)
14 {
15
16 #region
17 //--SPList, SPItem and SPFile throw exception.
18
19 SPSite mySite = new SPSite("http://mosstemplate:12345/sites/MyTeamSiteCollection1");
20 SPWeb myRootWeb = mySite.RootWeb;
21 SPWeb myWeb = myRootWeb.Webs["MyTeamSite1"];
22 SPList myList = myWeb.Lists["Tasks"];
23 SPList yourList = myWeb.Lists["NewDocLib1"];
24
25 int listCount = 0;
26 int itemCount = 0;
27 int fileCount = 0;
28
29 #region
30 try
31 {
32 //MyNewNewDocLib名字不存在
33 SPList testList = myWeb.Lists["MyNewNewDocLib"];
34 listCount++;
35 Console.WriteLine("Program doesn't throw exception. And the count of list is: {0}", listCount);
36 }
37 catch
38 {
39 Console.WriteLine("Exception: This list doesn't exist. And the count of list is: {0}", listCount);
40 }
41 #endregion
42
43 #region
44 try
45 {
46 //下标为100的Item不存在
47 SPItem testItem = myList.Items[100];
48 itemCount++;
49 Console.WriteLine("Program doesn't throw exception. And the count of item is: {0}", itemCount);
50 }
51 catch
52 {
53 Console.WriteLine("Exception: This item doesn't exist. And the count of item is: {0}", itemCount);
54 }
55 #endregion
56
57 try
58 {
59 //myFile名字不存在(即:没有此ServerRelativeUrl)
60 SPFile mFile = yourList.RootFolder.Files["sites/MyTeamSiteCollection1/MyTeamSite1/NewDocLib1/myFile"];
61 fileCount++;
62 Console.WriteLine("Program doesn't throw exception. And the count of file is: {0}", fileCount);
63 }
64 catch
65 {
66 Console.WriteLine("Exception: This file doesn't exist. And the count of file is: {0}", fileCount);
67 }
68 #endregion
69 }
70 }
71 }
72

经过我的测试,输出结果如下:Exception: This list doesn't exist. And the count of list is: 0

                                       Exception: This item doesn't exist. And the count of item is: 0

                                       Exception: This file doesn't exist. And the count of file is: 0

可以看出:在此三个级别中,用不存在的名字去获得对象时,程序会直接抛出异常。(Try{}中后面的语句将不会执行)。

区分在这几个级别中,用不存在的名字去获取对象时是否直接抛异常的作用是什么呢?

答案是:可以用这种方法去判断,以此为名的对象是否已经存在,如果存在,就不去在此创建此对象;如果不存在,则说明此名字唯一,那么就可以根据是否抛异常,分别在Try{}中或者Catch(){}中取创建此对象。【即:保证创建对象的唯一性,并且保证此唯一的对象一定会被创建成功】

原文地址:https://www.cnblogs.com/mingmingruyuedlut/p/1889691.html