3

I am trying to implement try/catch scope in my JSOM, but I am getting the error message in the question's title.

Below is my code:

        errScope = new SP.ExceptionHandlingScope(context);
        var scopeStart = errScope.startScope();

        var tryBlock = errScope.startTry();
            var targetList = context.get_web().get_lists().getByTitle("Announcements");
            //constructing caml
            var query = new SP.CamlQuery();
            var caml = "<View><Query><Where><Neq><FieldRef Name='Title'/>";
            caml += "<Value Type='Text'>Excluded</Value></Neq></Where></Query>";
            caml += "<RowLimit>2</RowLimit>";
            caml += "</View>";
            query.set_viewXml(caml);
            var listItems = targetList.getItems(query);
            results = context.loadQuery(listItems, 'Include(Title, Author, ID)')
            targetList = context.get_web().get_lists().getByTitle("Announcements");
        tryBlock.dispose();
        context.executeQueryAsync(_onSucceed, _Fail)

        var catchBlock = errScope.startCatch();
        catchBlock.dispose();



        var finallyBlock = errScope.startFinally();
        finallyBlock.dispose();

When it reaches the end of the try block, it throws the error:

Uncaught Error: Incorrect usage of exception handling scope.

Also I don't know what are the best practices to put in the catch block and finally block.

3

You should not add context.executeQueryAsync(_onSucceed, _Fail); inside the scope of exception. Wait until you dispose everything then call the method as it's not part of exception handling.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.