SharePoint Development in C # + Best way to handle error handling

I have been developing pages and web parts in SharePoint 2007/2010 over the past year. One of the things that I always include in my code is try / catch blocks to handle errors when an exception occurs. That way, if I have a web part that breaks down, I display an error message to the user in that particular web part, and not at the top of the page or the full page error. I am fine with this process, but I am looking for some input into my approach of using try / catch blocks (since I understand that there may be performance implications).

In general, I always use try / catch blocks when making database or web service calls. I even sometimes transfer my general C # code to try / catch blocks if there is complex logic that is implemented (and I could not check all the different cases). Sometimes I just have a little paranoia and we complete all the code in the entire web part with try / catch.

Can I get some feedback on using try / catch blocks for general C # development, and in particular, C # development in the SharePoint world? Also, I would like to understand how to effectively implement try / catch in my C # code when developing SharePoint solutions (is there a more global way to do this?)

Thanks in advance.

+4
source share
1 answer

With error handling in the SharePoint world, you will usually focus on performance. If you're worried about performance, you'll want exceptions to bubble as much as possible. I usually wrap input methods with try / catch blocks. For example, event handlers in asp.net forms, web parts, user controls, etc. Then I will wrap the code with utilities, data access, etc., When I want to throw custom exception types, configure an error message, etc.

Unfortunately, I did not find a global way to handle errors in SharePoint. I applied the HandleError utility method to the web parts that I pass in 'this' and the exception object. (The WSPBuilder Extensions web part element actually has a good implementation of this embedded file.) However, the try / catch blocks themselves are still local to the web part event handlers.

+1
source

All Articles