How do I handle SharePoint exceptions?

前端 未结 4 1376
天命终不由人
天命终不由人 2021-01-07 00:24

I`m new to SharePoint, so I guess how do I need to handle exceptions? When I write custom code do I have to check for them or maybe, if they are thrown, they automatically g

4条回答
  •  没有蜡笔的小新
    2021-01-07 01:27

    I'm going to assume you are using the SharePoint object model and not its web services as you haven't stated which, and the object model is typically used as it provides more functionality.

    SharePoint uses its own custom SPException class to throw exceptions as well as several standard .NET exception classes. Unfortunately the SDK documentation does not mention which methods throw exceptions and when this might happen. Your own experiences and Reflector are best for learning this.

    When first learning you will probably cause exceptions to be thrown quite often and it can be quite unpredictable to know when and why this is happening. Those thrown by SPException generally have helpful messages however the standard .NET exceptions that SharePoint also throws (such as ArgumentOutOfRangeException) give very little detail. Therefore to help debugging it is generally a good idea to keep your methods short and to the point so it's easy to find why an exception might be occurring. You only need to check for them if there is a good reason to so letting them bubble up unless you can rethrow and add more detail (suggested by Peter) is a good approach to ensure correctness and keep your code clean.

    SharePoint usually does not automatically log exceptions. Sometimes you will find them in the ULS (a.k.a. Trace Log) files but generally when logged they don't give any more information than the exceptions you can catch yourself. There are many options for setting up your own logging as already mentioned here. You can also find a great list at SharePoint Dev Wiki.

提交回复
热议问题