How to: Use Exception Handling Scope
SharePoint 2010
Published: May 2010
Available in SharePoint Online
The following example shows how to implement exception handling scope through the ExceptionHandlingScope class (JavaScript: ExceptionHandlingScope), and its StartTry(), StartCatch(), and StartFinally() methods (in JavaScript, startTry(), startCatch(), and startFinally()).
The example tries to return a list with a specified title and, if the list exists, modifies its description and then enables folder creation. If the list does not exist, the example creates the list and then enables folder creation.
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(); } } }
Client Context implements IDisposable
It should be noted that according to Andrew Connell, the Client Context should be wrapped in a using, or disposed of properly. See:
http://www.sharepointpromag.com/content1/topic/sharepoint-2010-s-client-object-model/catpath/sharepoint
http://www.sharepointpromag.com/content1/topic/sharepoint-2010-s-client-object-model/catpath/sharepoint
- 2/27/2012
- rdcpro
ServerErrorCode and erro details
Hi,
It might be interesting to mention that properties such as the ServerErrorCode are only available after ExecuteQuery() or ExecuteQueryAsync() gets fired. This might sound obvious but we could be tempted to write:
using (scope.StartScope())
{
using (scope.StartTry())
{
List oList = clientContext.Web.Lists.GetByTitle("My List");
oList.Description = "In Try Block";
oList.Update();
}
using (scope.StartCatch())
{
switch(scope.ServerErrorCode)
{
case....
}
}
}
which is actually not possible since it will always be equals to -1.
It might be interesting to mention that properties such as the ServerErrorCode are only available after ExecuteQuery() or ExecuteQueryAsync() gets fired. This might sound obvious but we could be tempted to write:
using (scope.StartScope())
{
using (scope.StartTry())
{
List oList = clientContext.Web.Lists.GetByTitle("My List");
oList.Description = "In Try Block";
oList.Update();
}
using (scope.StartCatch())
{
switch(scope.ServerErrorCode)
{
case....
}
}
}
which is actually not possible since it will always be equals to -1.
- 11/21/2010
- stephane eyskens
More Details
More details can be found on http://ranaictiu-technicalblog.blogspot.com/2010/08/sharepoint-2010-exception-handling.html
- 8/11/2010
- Sohel Rana