How do I handle SharePoint exceptions?

前端 未结 4 1354
天命终不由人
天命终不由人 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:02

    We allow all our exceptions to bubble up and use a custom HTTP module to handle all exceptions. From here, we log to the ULS logs and also to the trace logs. This is mostly lifted from SharePoint MVP Chris O'brien's Code

    We only catch exceptions if we feel we can add extra value to the exception, and then re-throw the error, allowing the http module to pick it up.

    0 讨论(0)
  • 2021-01-07 01:05

    The general rule with exceptions is you should only catch the exceptions you know how to handle yourself correctly - I don't know if sharepoint logs unhandled exceptions itself, but I think it converts unhandled exceptions into the appropriate SOAP exception in the various web services so it's a good bet that you won't crash the whole app.

    0 讨论(0)
  • 2021-01-07 01:21

    Use this to write to the log: http://msdn.microsoft.com/en-us/library/aa979522.aspx

    Unless you handle your exceptions the application will most likely break. Some errors such as those generated in event receivers will not "break" the application (except for terminating the event receiver). Those, along with all other uncatched errors will end up in the log unless you've changed the settings in central admin.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题