例外処理範囲を使用する

最終更新日: 2010年2月9日

適用対象: SharePoint Foundation 2010

SharePoint Online で使用可能

次の例は、ExceptionHandlingScope クラス (JavaScript: ExceptionHandlingScope) と、このクラスの StartTry() メソッドと StartCatch() メソッドと StartFinally() メソッド (JavaScript では、startTry()startCatch()startFinally()) による例外処理範囲の実装方法を示しています。

この例は、指定されたタイトルのリストを返そうと試み、目的のリストが存在する場合は、リストの記述内容に変更を加え、フォルダーを作成できるようにします。目的のリストが存在しない場合は、リストを作成し、フォルダーを作成できるようにします。

using System;
using Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
    class ExampleExceptionHandlingScope
    {        
        // Update the description of specified list and also 
        // enable folder creation.  There's a possibility that the specified 
        // list does not exist.
        static void Main()
        {
            ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");            

            ExceptionHandlingScope scope = new ExceptionHandlingScope(clientContext);            

            using (scope.StartScope())
            {
                using (scope.StartTry())
                {
                    List oList = clientContext.Web.Lists.GetByTitle("My List");
                    oList.Description = "In Try Block";
                    oList.Update();
                }

                using (scope.StartCatch())
                {                    
                    // Assume that if there's an exception, it can only be 
                    // because there is no list with the specified title, so
                    // create the list.
                    ListCreationInformation listCreateInfo = new ListCreationInformation();
                    listCreateInfo.Title = "My List";
                    listCreateInfo.Description = "In Catch Block";
                    listCreateInfo.TemplateType = (int)ListTemplateType.Announcements;
                    List oList = clientContext.Web.Lists.Add(listCreateInfo);                    
                }

                using (scope.StartFinally())
                {                    
                    List oList = clientContext.Web.Lists.GetByTitle("My List");
                    oList.EnableFolderCreation = true;
                    oList.Update();                     
                }
            }

            clientContext.ExecuteQuery();            
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples

    Class ExampleExceptionHandlingScope

       ' Update the description of specified list and also 
       ' enable folder creation.  There's a possibility that the specified 
       ' list does not exist.
        Shared Sub Main()

            Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection")

            Dim scope As New ExceptionHandlingScope(clientContext)

            Using scope.StartScope()

                Using scope.StartTry()

                    Dim oList As List = clientContext.Web.Lists.GetByTitle("My List")
                    oList.Description = "In Try Block"
                    oList.Update()

                End Using

                Using scope.StartCatch()

                   ' Assume that if there's an exception, it can only be 
                   ' because there is no list with the specified title, so
                   ' create the list.
                    Dim listCreateInfo As New ListCreationInformation()
                    listCreateInfo.Title = "My List"
                    listCreateInfo.Description = "In Catch Block"
                    listCreateInfo.TemplateType = CInt(ListTemplateType.Announcements)
                    Dim oList As List = clientContext.Web.Lists.Add(listCreateInfo)

                End Using

                Using scope.StartFinally()

                    Dim oList As List = clientContext.Web.Lists.GetByTitle("My List")
                    oList.EnableFolderCreation = True
                    oList.Update()

                End Using
            End Using

            clientContext.ExecuteQuery()

        End Sub
    End Class
End Namespace

関連項目

概念

[方法] 条件範囲を使用する

一般的なプログラミング作業

その他の技術情報

クライアント クラス ライブラリ

ECMAScript クラス ライブラリ