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{}中后面的语句将不会执行)。