3

I have an event receiver that copies items to another list. I however need to show errors to the user that the item did not copy.

try
                    {
                       //copy code works here
                    }
                    catch (Exception ex)
                    {
                        ILogger logger = new ULSLogger();
                        logger.LogError(ex.Message, ex);
                    }

Would it be good coding practice to change the catch block to the below:

    catch (Exception ex)
                        {
                            ILogger logger = new ULSLogger();
                            logger.LogError(ex.Message, ex);

                            properties.Cancel = true;
                            properties.ErrorMessage = "Items not copied";
                        }
2
  • What event is your event receiver attached to? (ItemAdded, ItemUpdated, etc.) Nov 6 '13 at 22:00
  • It's attached to the ItemUpdated
    – Chester238
    Nov 7 '13 at 5:51
3

You can use an SPException so that the masterpage is wrapped in the error.

catch(Exception ex)
{
 throw new SPException("Items not copied + "\n" + ex.Message");
}

If you use SPException, the error will also be logged to the ULS which is very useful and good practice.

http://blogs.msdn.com/b/uksharepoint/archive/2009/01/21/dev-tip-throwing-spexceptions.aspx

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.