none
[CSOM] Why ExceptionHandlingScope can not work. RRS feed

  • Question

  • Hi,

    I use the CSOM to access sharepoint data, and try to use Exception Handling Scope to deal with the exception (ref: https://msdn.microsoft.com/en-us/library/office/ee534976(v=office.14).aspx )

    The code is following:

    [TestMethod]
            public void TestErrorHandleScope()
            {
                using (ClientContext context = new ClientContext("https://arcservedevqa.sharepoint.com/sites/"))
                {
                    string pwd = "mypassword";
                    char[] pwdCharArray = pwd.ToCharArray();
                    SecureString password = new SecureString();
                    foreach (var a in pwdCharArray)
                    {
                        password.AppendChar(a);
                    }
                    context.Credentials = new SharePointOnlineCredentials("test@arcservedevqa.onmicrosoft.com", password);
    
                    var folder = context.Web.GetFolderByServerRelativeUrl("/sites/MySite/Doc/112/"); // the folder not exists
    
                    ExceptionHandlingScope scope = new ExceptionHandlingScope(context);
    
                    using (scope.StartScope())
                    {
                        using (scope.StartTry())
                        {
                            context.Load(folder);
                        }
    
                        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.
    
                        }
    
                        using (scope.StartFinally())
                        {
    
                        }
                    }
                    try
                    {
                        context.Load(context.Web, w => w.Title);
                        context.ExecuteQuery();
                    }
                    catch (Exception ex)
                    {
    
                    }
                }
            }

    From the ref url, I think the exception should not be thrown, instead, the exception information should be saved to the ExceptionHandlingScope object.

    So could someone can help:

    1. Why using the exception handle scope, it doesn't work.

    2. If there is something wrong with my code, please help correct me.

    Note: The CSOM library version is : 16.1.6906.1200.

    And I use the CSOM to access the SharePoint online of Office 365.

    Thanks,

    Haiyang.

    Thursday, October 26, 2017 9:59 AM