Answered by:
Best practise in handling exception for a custom webpart

Question
-
Is there any best practise in handling exception in the event there is some error to the custom webpart?
Thanks in advance.- Moved by Mike Walsh FIN Sunday, June 21, 2009 9:13 AM prog q (From:SharePoint - General Question and Answers and Discussion)
Sunday, June 21, 2009 3:26 AM
Answers
-
Hi,
Hope those two posts help you:
http://www.sharepointsecurity.com/blog/sharepoint/sharepoint-2007-development/webpart-exception-error-handling/ (WebPart Exception / Error Handling)
http://www.codeproject.com/KB/sharepoint/Errorhandling_in_Webparts.aspx (Error handling in Webparts for SharePoint 2007)
Mahdi Abdulhamid - MCTS http://www.devexpert.net
- Proposed as answer by Moss-dever Tuesday, June 23, 2009 3:58 AM
- Marked as answer by Aaron Han - MSFT Thursday, June 25, 2009 10:18 AM
Sunday, June 21, 2009 8:46 AM -
To add more detail to the responses above, I would:
* Catch specific exceptions, then catch general exception
* Display a friendly error message to the end user, in a label rather than displaying the SharePoint error page.
* Handle your own exceptions, don't rely on SharePoint or another layer to handle expections for you.
* Log the error in a central place and be consistent. You can log errors to the event view or to the SharePoint Trace Log, or even a SharePoint list. Just make sure its consistent and the right people can access it. For example: with hosted solutions don't log to the SharePoint Trace Log or Event Viewer if you don't have access to it. SharePoint list is a flexible option as you can use alerts to be notified of any error messages.
* Try to capture any inputs or helpful diagnostic information so the error can be reproduced easily.
Hope this helps
Dave
My SharePoint Blog - http://www.davehunter.co.uk/blog- Edited by Dave Hunter Monday, June 22, 2009 1:31 PM
- Proposed as answer by Moss-dever Tuesday, June 23, 2009 3:58 AM
- Marked as answer by Aaron Han - MSFT Thursday, June 25, 2009 10:18 AM
Monday, June 22, 2009 12:52 PM
All replies
-
Hi,
Hope those two posts help you:
http://www.sharepointsecurity.com/blog/sharepoint/sharepoint-2007-development/webpart-exception-error-handling/ (WebPart Exception / Error Handling)
http://www.codeproject.com/KB/sharepoint/Errorhandling_in_Webparts.aspx (Error handling in Webparts for SharePoint 2007)
Mahdi Abdulhamid - MCTS http://www.devexpert.net
- Proposed as answer by Moss-dever Tuesday, June 23, 2009 3:58 AM
- Marked as answer by Aaron Han - MSFT Thursday, June 25, 2009 10:18 AM
Sunday, June 21, 2009 8:46 AM -
Moving to the Programming forum.
WSS FAQ sites: http://wssv2faq.mindsharp.com and http://wssv3faq.mindsharp.com
Total list of WSS 3.0 / MOSS 2007 Books (including foreign language) http://wssv3faq.mindsharp.com/Lists/v3%20WSS%20FAQ/V%20Books.aspxSunday, June 21, 2009 9:12 AM -
I think exception handling is not a topic specific to webpart or any component. So just follow the best practices for implementing exception handling, it will itself manage and handle the exceptions in your code.
But still go through below discussion thread, it will help u.
http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/496497e2-9224-4574-8cad-9cfacef238e9/
Thanks & Regards
Rakesh Narayan LalSunday, June 21, 2009 2:27 PM -
keep your code in try catch loop, And dont thow error as it consume more memory, Simple keep your code in try block anddont handle in catch block, in this case your webpart wont throw any error.
try{}catch{}For more reference : http://msdn.microsoft.com/en-us/library/seyhszts.aspx Best practices for Handling Exceptions.Sunday, June 21, 2009 4:51 PM -
To add more detail to the responses above, I would:
* Catch specific exceptions, then catch general exception
* Display a friendly error message to the end user, in a label rather than displaying the SharePoint error page.
* Handle your own exceptions, don't rely on SharePoint or another layer to handle expections for you.
* Log the error in a central place and be consistent. You can log errors to the event view or to the SharePoint Trace Log, or even a SharePoint list. Just make sure its consistent and the right people can access it. For example: with hosted solutions don't log to the SharePoint Trace Log or Event Viewer if you don't have access to it. SharePoint list is a flexible option as you can use alerts to be notified of any error messages.
* Try to capture any inputs or helpful diagnostic information so the error can be reproduced easily.
Hope this helps
Dave
My SharePoint Blog - http://www.davehunter.co.uk/blog- Edited by Dave Hunter Monday, June 22, 2009 1:31 PM
- Proposed as answer by Moss-dever Tuesday, June 23, 2009 3:58 AM
- Marked as answer by Aaron Han - MSFT Thursday, June 25, 2009 10:18 AM
Monday, June 22, 2009 12:52 PM -
Hi,
just adding my own thoughts:
IMHO better to go for EntLib 4.0 Exception Handling Block and Logging App Block.
Create custom exception policy and catch it whenever, wherever necessary
We need to add few entries in the web.config file.
Also check for
http://www.pnpguidance.net/
http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/06/19/EnterpriseLibraryExceptionHandlingApplicationBlockScreencast.aspx
http://davidhayden.com/blog/dave/category/70.aspx?Show=All
http://geeksconnected.com/msalem/Presentations/Introduction%20to%20Microsoft%20Enterprise%20Library/Introduction%20to%20Microsoft%20Enterprise%20Library.pptx
PrasadWTMonday, June 22, 2009 1:26 PM -
Thanks for your replies. I also found this which is about the same as you all.
http://sharepointexception.wordpress.com/2008/09/14/bullet-proof-coding-sharepoint-error-checking-and-exception-handling/Thursday, June 25, 2009 1:15 PM