views:

776

answers:

5

Hello.

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 get logged and don't break the app?

If not, then how do I log them?

Thank you!

Edit: And how should I log those exceptions?

A: 

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.

1800 INFORMATION
+1  A: 

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.

JMD
You say "application will most likely break", but after that "all other uncatched errors will end up in the log".Could you be more specific, please?
Janis Veinbergs
http://channel9.msdn.com/posts/akMSFT/pp-SharePoint-Development-Guidance-v2-Whats-in-Drop-8/ contains a good description of exception handling in SharePoint.
JMD
A: 

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.

Peter Walke
+1  A: 

Look in this article "Simplifying SharePoint debugging by creating the troubleshooting toolbox" http://sharepointmagazine.net/technical/development/getting-started-with-sharepoint-programming-simplifying-sharepoint-debugging-by-creating-the-troubleshooting-toolbox. It has several good tips on logging, debugging etc. when you develop app for SharePoint.

Dmitri Kouminov
A: 

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.

Alex Angas