<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>SharePoint Exception Handling</title>
	<atom:link href="http://sharepointexception.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://sharepointexception.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Mon, 20 Oct 2008 21:42:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='sharepointexception.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>SharePoint Exception Handling</title>
		<link>http://sharepointexception.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://sharepointexception.wordpress.com/osd.xml" title="SharePoint Exception Handling" />
	<atom:link rel='hub' href='http://sharepointexception.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Bullet proof coding &#8211; SharePoint Error checking and Exception handling</title>
		<link>http://sharepointexception.wordpress.com/2008/09/14/bullet-proof-coding-sharepoint-error-checking-and-exception-handling/</link>
		<comments>http://sharepointexception.wordpress.com/2008/09/14/bullet-proof-coding-sharepoint-error-checking-and-exception-handling/#comments</comments>
		<pubDate>Sun, 14 Sep 2008 12:40:02 +0000</pubDate>
		<dc:creator>burlo</dc:creator>
				<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[Burlo]]></category>
		<category><![CDATA[SharePoint Best Practices]]></category>
		<category><![CDATA[SharePoint Code Reviews]]></category>
		<category><![CDATA[SharePoint Error Handling]]></category>
		<category><![CDATA[SharePoint Exception Handling]]></category>

		<guid isPermaLink="false">http://sharepointexception.wordpress.com/?p=4</guid>
		<description><![CDATA[Error and exception handling is very important in creating a robust and high quality system and more importantly for trouble shooting. Error and exception handling must be done correctly so that potential issues with the system can be diagnosed quickly.  There is nothing worst then an error being thrown and you cannot figure out what [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sharepointexception.wordpress.com&amp;blog=4735714&amp;post=4&amp;subd=sharepointexception&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin:6pt 0;"><strong></strong></p>
<p class="MsoNormal" style="margin:6pt 0;"><span style="font-size:small;font-family:Calibri;">Error and exception handling is very important in creating a robust and high quality system and more importantly for trouble shooting. Error and exception handling must be done correctly so that potential issues with the system can be diagnosed quickly. <span> </span>There is nothing worst then an error being thrown and you cannot figure out what has caused the problem because the log file is showing a generic message. In addition, developers tend to develop and test their components in a manner that will always work. As soon as an end user tests or uses the system they will tend to use the system in all sorts of ways that will result in the system breaking. When this happen if the code base does not use rigorous error checking and exception handling mechanisms it becomes very difficult to diagnose the problem and gives no chance to the end user or administrator clues<span>  </span>on<span>  </span>how to rectify the issue. It will require a developer to debug the code (that is if he/she can reproduce the error in a SharePoint development environment) to understand what is actually causing the error.</span></p>
<p class="MsoNormal" style="margin:6pt 0;"><span style="font-size:small;font-family:Calibri;">Software components should be develop to a commercial quality standard and must be robust enough to except undesirable situations without breaking the system. Employing a test driven approach for unit testing your components is a good way in developing code to see the effects and characteristics of your components when undesirable situation occur. In addition, develop your system to be smart, check for undesirable circumstance and if possible rectify the problem by taking a different course of action. The following suggestions are best practices in developing code related to MOSS development in regards to error checking and exception handling. The following suggestions are a good start in formulating a template for your SharePoint code reviews.</span></p>
<p class="MsoNormal" style="margin:6pt 0;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:6pt 0;"><strong><span style="font-size:14pt;"><span style="font-family:Calibri;">The Basics</span></span></strong></p>
<p class="MsoListParagraphCxSpFirst" style="text-indent:-.25in;margin:6pt 0 0 .5in;"><span style="font-family:Symbol;"><span><span style="font-size:small;">·</span><span style="font-family:&quot;">         </span></span></span><span style="font-size:small;font-family:Calibri;">Always display a high level abstract message to the user when an error occurs or an exception is thrown rather than a low level exception message that the user will not understand. The low level specific error message should be written to the log file.</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span style="font-family:Symbol;"><span><span style="font-size:small;">·</span><span style="font-family:&quot;">         </span></span></span><span style="font-size:small;font-family:Calibri;">Check objects, values, properties etc before using them so that unexpected exceptions are not thrown. <span> </span>For example </span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">         &#8211; <span style="font-size:small;">When obtaining an object always check the object is not null before using it.</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">         &#8211; <span style="font-size:small;">Always check strings to make sure they are not empty or null i.e. String.IsNullOrEmpty(“aString”)</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 1.25in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span style="font-family:Symbol;"><span><span style="font-size:small;">·</span><span style="font-family:&quot;">         </span></span></span><span style="font-size:small;font-family:Calibri;">Never use try catch blocks to control the logic of your code, it is more efficient to use if/else statements and only throw exceptions when nothing else can be done. </span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span style="font-family:Symbol;"><span><span style="font-size:small;">·</span><span style="font-family:&quot;">         </span></span></span><span style="font-size:small;font-family:Calibri;">When casting always check the object can be cast to the particular object. For example:</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">if(MyObject is String)</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">{</span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:.5in;margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">// Cast object your object</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">}</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span style="font-family:Symbol;"><span><span style="font-size:small;">·</span><span style="font-family:&quot;">         </span></span></span><span style="font-size:small;font-family:Calibri;">Always log an error when it occurs and write your log message in detail around the specific error. Don’t just log the error by calling the Message property on the exception object. Add context around the error for the specific operation it is trying to do.</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span style="font-family:Symbol;"><span><span style="font-size:small;">·</span><span style="font-family:&quot;">         </span></span></span><span style="font-size:small;font-family:Calibri;">Always catch the specific exception.<span>  </span>Developers tend to be lazy and catch the generic exception (i.e. System.Exception Object) around a large code block. The issue here is that although you have caught the exception it still becomes very difficult to determine what really cause the problem because the error message will be generic.</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoListParagraphCxSpLast" style="text-indent:-.25in;margin:0 0 6pt .5in;"><span style="font-family:Symbol;"><span><span style="font-size:small;">·</span><span style="font-family:&quot;">         </span></span></span><span style="font-size:small;font-family:Calibri;">Never throw an exception inside a constructor as it can be very difficult to find where the exception is being thrown and always catch all exceptions in a Dispose method if you are defining your own.</span></p>
<p class="MsoNormal" style="margin:6pt 0;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoListParagraphCxSpFirst" style="text-indent:-.25in;margin:6pt 0 0 .5in;"><span style="font-family:Symbol;"><span><span style="font-size:small;">·</span><span style="font-family:&quot;">         </span></span></span><span style="font-size:small;font-family:Calibri;">Try/Catch blocks should never contain empty catch handlers. </span></p>
<p class="MsoListParagraphCxSpLast" style="margin:0 0 6pt .5in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:6pt 0;"><span style="font-family:Calibri;"><strong><span style="font-size:14pt;">Logging</span></strong></span></p>
<p class="MsoNormal" style="margin:6pt 0;"><span style="font-size:small;font-family:Calibri;">An important part of exception handling is providing detailed information around the error in order to trouble shooting errors that may arise during development, UAT and/or Production. Generally, when developing a MOSS solution I like to use the MOSS logger (i.e. PortalLog) so as to keep all diagnostic and error information in one log file (i.e. in the 12 hive under the LOGS directory) and no extra dll’s are needed to be installed. The only limitation to using the MOSS logger is that you cannot set the log event level e.g. critical, high, medium, low it will always display the error level as high. The MOSS logger uses the PortalLog class and logs a message by calling the LogString method as shown below:</span></p>
<p class="MsoNormal" style="margin:6pt 0;"><span style="font-size:small;"><span style="font-family:Calibri;"><span>            </span>Microsoft.Office.Server.Diagnostics.PortalLog.LogString(&#8220;log your error&#8221;);</span></span></p>
<p class="MsoNormal" style="margin:6pt 0;"><span style="font-size:small;font-family:Calibri;">The MOSS logger is located in the Microsoft.Office.Server.dll and therefore it is only available with the MOSS install, not WSS 3. If you want more control and flex ability use the Enterprise library logger.</span></p>
<p class="MsoNormal" style="margin:6pt 0;"><strong><span style="font-size:14pt;"><span style="font-family:Calibri;">SharePoint Exception handling</span></span></strong></p>
<p class="MsoListParagraphCxSpFirst" style="text-indent:-.25in;margin:6pt 0 0 .5in;"><span style="font-family:Symbol;"><span><span style="font-size:small;">·</span><span style="font-family:&quot;">         </span></span></span><span style="font-size:small;font-family:Calibri;">In SharePoint always throw SPException Objects to the top of the UI call stack and pass user friendly messages to the SPException object as this will be the message displayed to the user in the browser. Do not pass the Message property of the specific exception into the SPExcetption object as the user may not understand such a technically messages.</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span style="font-family:Symbol;"><span><span style="font-size:small;">·</span><span style="font-family:&quot;">         </span></span></span><span style="font-size:small;font-family:Calibri;">When exceptions occur in custom feature receivers catch the specific exception, log the error and throw a SPException object to the top of the UI stack as shown below.</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">public class MyFeatureReciver : SPFeatureReceiver</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">{</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">   <span style="font-size:small;">public override void FeatureActivated(SPFeatureReceiverProperties properties)</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">   <span style="font-size:small;">{</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">      <span style="font-size:small;">try</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">      <span style="font-size:small;">{</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;"><span style="font-family:Calibri;">         </span><span style="font-size:small;"><span style="font-family:Calibri;">// Do Something</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;"><span style="font-family:Calibri;">      </span><span style="font-size:small;"><span style="font-family:Calibri;">}</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">      <span style="font-size:small;">Catch(ArgumentException)</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">      <span style="font-size:small;">{</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;"><span style="font-family:Calibri;">         </span><span style="font-size:small;"><span style="font-family:Calibri;">PortalLog.LogString(&#8220;log Log detailed message.”);</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;"><span style="font-family:Calibri;">         </span><span style="font-size:small;"><span style="font-family:Calibri;">throw new SPException(“Enter user friendly message.”);</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;"><span style="font-family:Calibri;">      </span><span style="font-size:small;"><span style="font-family:Calibri;">}</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">   <span style="font-size:small;">}</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;"><span style="font-family:Calibri;">}</span></span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-size:small;font-family:Calibri;">If you only catch and log the exception without throwing the SPException<span>  </span>then the<span>  </span>feature will complete the requested feature <span> </span>activation or de-activation (which ever the user is currently requesting) instead of aborting the feature activation or de-activation.</span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .25in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoListParagraph" style="text-indent:-.25in;margin:6pt 0 6pt .25in;"><span style="font-family:Symbol;"><span><span style="font-size:small;">·</span><span style="font-family:&quot;">         </span></span></span><span style="font-size:small;font-family:Calibri;">When developing a custom feature receiver in which you are obtaining an SPSite or SPWeb object via the Parent object make sure you check if the parent object can be casted to a SPSite or SPWeb object (depending on the scope of your feature) before casting it. An example is provided below.</span></p>
<p class="MsoListParagraph" style="text-indent:-.25in;margin:6pt 0 6pt .25in;"><span style="font-family:Calibri;"><span style="font-size:small;">      <span style="font-size:small;">public class MyFeatureReciver : SPFeatureReceiver</span></span></span></p>
<p class="MsoListParagraph" style="text-indent:-.25in;margin:6pt 0 6pt .25in;"><span style="font-family:Calibri;"><span style="font-size:small;">      <span style="font-size:small;">{</span></span></span></p>
<p class="MsoListParagraph" style="text-indent:-.25in;margin:6pt 0 6pt .25in;"><span style="font-family:Calibri;"><span style="font-size:small;">      <span style="font-size:small;">public override void FeatureActivated(SPFeatureReceiverProperties properties)</span></span></span></p>
<p class="MsoListParagraph" style="text-indent:-.25in;margin:6pt 0 6pt .25in;"><span style="font-family:Calibri;"><span style="font-size:small;">      <span style="font-size:small;">{</span></span></span></p>
<p class="MsoListParagraph" style="text-indent:-.25in;margin:6pt 0 6pt .25in;"><span style="font-size:small;"><span style="font-family:Calibri;">         </span><span style="font-size:small;"><span style="font-family:Calibri;">Object parent = properties.Feature.Parent;</span></span></span></p>
<p class="MsoListParagraph" style="text-indent:-.25in;margin:6pt 0 6pt .25in;"><span style="font-size:small;"><span style="font-family:Calibri;">         i</span><span style="font-size:small;"><span style="font-family:Calibri;">f(parent is SPWeb)</span></span></span></p>
<p class="MsoListParagraph" style="text-indent:-.25in;margin:6pt 0 6pt .25in;"><span style="font-size:small;"><span style="font-family:Calibri;">         </span><span style="font-size:small;"><span style="font-family:Calibri;">{</span></span></span></p>
<p class="MsoListParagraph" style="text-indent:-.25in;margin:6pt 0 6pt .25in;"><span style="font-size:small;"><span style="font-family:Calibri;">            </span><span style="font-size:small;"><span style="font-family:Calibri;">SPWeb spWeb = (SPWeb)parent;</span></span></span></p>
<p class="MsoListParagraph" style="text-indent:-.25in;margin:6pt 0 6pt .25in;"><span style="font-size:small;"><span style="font-family:Calibri;">         </span><span style="font-size:small;"><span style="font-family:Calibri;">}</span></span></span></p>
<p class="MsoListParagraph" style="text-indent:-.25in;margin:6pt 0 6pt .25in;"><span style="font-size:small;"><span style="font-family:Calibri;">         e</span><span style="font-size:small;"><span style="font-family:Calibri;">lse</span></span></span></p>
<p class="MsoListParagraph" style="text-indent:-.25in;margin:6pt 0 6pt .25in;"><span style="font-size:small;"><span style="font-family:Calibri;">        </span><span style="font-size:small;"><span style="font-family:Calibri;">{</span></span></span></p>
<p class="MsoListParagraph" style="text-indent:-.25in;margin:6pt 0 6pt .25in;"><span style="font-size:small;"><span style="font-family:Calibri;">            </span><span style="font-size:small;"><span style="font-family:Calibri;">PortalLog.LogString(&#8220;log Log detailed message.”);</span></span></span></p>
<p class="MsoListParagraph" style="text-indent:-.25in;margin:6pt 0 6pt .25in;"><span style="font-size:small;"><span style="font-family:Calibri;">            </span><span style="font-size:small;"><span style="font-family:Calibri;">throw new SPException(“Enter user friendly message.”);</span></span></span></p>
<p class="MsoListParagraph" style="text-indent:-.25in;margin:6pt 0 6pt .25in;"><span style="font-family:Calibri;"><span style="font-size:small;">         <span style="font-size:small;">}</span></span></span></p>
<p class="MsoListParagraph" style="text-indent:-.25in;margin:6pt 0 6pt .25in;"><span style="font-size:small;"><span style="font-family:Calibri;">      </span><span style="font-size:small;"><span style="font-family:Calibri;"><span> </span>}</span></span></span></p>
<p class="MsoListParagraph" style="text-indent:-.25in;margin:6pt 0 6pt .25in;"><span style="font-family:Calibri;"><span style="font-size:small;">      <span style="font-size:small;">}</span></span></span></p>
<p class="MsoListParagraphCxSpFirst" style="margin:6pt 0 0 .5in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span style="font-family:Symbol;"><span><span style="font-size:small;">·</span><span style="font-family:&quot;">         </span></span></span><span style="font-size:small;font-family:Calibri;">When developing webparts which have custom user defined properties available in the web part editing pane, the webpart should always check the value and if it is not valid display an error message in the web part. This applies to the situation when a user changes the value in the web part editing pane. If the value is invalid the user should be notified after they click apply, so they can immediately change the value to a correct value.</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span style="font-family:Symbol;"><span><span style="font-size:small;">·</span><span style="font-family:&quot;">         </span></span></span><span style="font-size:small;font-family:Calibri;">When developing web parts that depend on data from a SPLists you should always catch the UnauthorizedAccessException object that will be thrown if the user does not have permissions. Web parts will be accessed by many different people with different permissions and in some situations users may not have the required permissions for particular lists. The developer may want to catch the exception and display a message in the web part or implement some type of action i.e. display a request for access control. In addition, you will also have to set SPSecurity.CatchAccessDeniedException = false so that the platform does not catch the access denied exception. </span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span style="font-family:Symbol;"><span><span style="font-size:small;">·</span><span style="font-family:&quot;">         </span></span></span><span style="font-size:small;font-family:Calibri;">When accessing lists (SPList) or list items (SPListItem) always catch the appropriate exception which will be thrown if the list or list item does not exist. If you access a list a list or list item via the collection key and the list or list item does not exist an ArgumentExecption will be thrown. This is illustrated below.</span></p>
<p class="MsoListParagraphCxSpLast" style="margin:0 0 6pt .5in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-size:small;font-family:Calibri;">try</span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-size:small;font-family:Calibri;">{</span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">   <span style="font-size:small;">spList = spWeb.List[“My WebSite”];</span></span></span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">   <span style="font-size:small;">// or</span></span></span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">   <span style="font-size:small;">spListItem = spList.Items[“My Item”];</span></span></span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-family:Calibri;"><span style="font-size:small;"><span style="font-size:small;">}</span></span></span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">c<span style="font-size:small;">atch(ArgumentException)</span></span></span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-size:small;font-family:Calibri;">{</span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-size:small;"><span style="font-family:Calibri;">   </span><span style="font-size:small;"><span style="font-family:Calibri;">// Log and take appropriate action for the exception</span></span></span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-size:small;font-family:Calibri;">}</span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-size:small;font-family:Calibri;">If you access a list or list item via the relative server URL and the list or list item does not exist a FileNotFoundExecption object will be thrown . This is illustrated below.</span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-size:small;font-family:Calibri;">try</span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-size:small;font-family:Calibri;">{</span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">   <span style="font-size:small;">spList = spWeb.GetList(“/Lists/MyList”);</span></span></span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">   <span style="font-size:small;">// or</span></span></span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">   <span style="font-size:small;">spListItem = spWeb.GetListItem(“/Lists/MyList/MyItem”);</span></span></span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-size:small;font-family:Calibri;">}</span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">c<span style="font-size:small;">atch(FileNotFoundException)</span></span></span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-size:small;font-family:Calibri;">{</span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-size:small;"><span style="font-family:Calibri;">   </span><span style="font-size:small;"><span style="font-family:Calibri;">// Log and take appropriate action for the exception</span></span></span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .5in;"><span style="font-size:small;font-family:Calibri;">}</span></p>
<p class="MsoNormal" style="margin:6pt 0;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoListParagraphCxSpFirst" style="margin:6pt 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">Otherwise, in some situations you may want to check if the list item exists before accessing it, an example is shown below.</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span><span style="font-size:small;font-family:Calibri;"> </span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">if(spList.Fields.ContainsField(“MyItem”);</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">{</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;"><span style="font-family:Calibri;">   </span><span style="font-size:small;"><span style="font-family:Calibri;">spListItem = spList.Items[“My Item”];</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">}</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">e<span style="font-size:small;">lse</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">{</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;"><span style="font-family:Calibri;">   </span><span style="font-size:small;"><span style="font-family:Calibri;">// Take appropriate action</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">}</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">However if you want to check if a list exists you will have to enumerate through the <span> </span>web site’s Lists collection and check if it exits, there is no method in the Object model that does this so you may want to extend the SPList class to accommodate this.</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span style="font-family:Symbol;"><span><span style="font-size:small;">·</span><span style="font-family:&quot;">         </span></span></span><span style="font-size:small;font-family:Calibri;">Never dispose of SPSite or SPWeb object inside of a try block, if an exception gets thrown before the Dispose method it will not dispose of the object appropriately, leading to memory leaks,. Best practice is to wrap your code in a using block when creating these objects, for example</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">Using (SPSite site collection = new SPSite(</span><a href="http://localhost/"><span style="font-size:small;color:#0000ff;font-family:Calibri;">http://localhost</span></a><span style="font-size:small;font-family:Calibri;">)</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">{</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;"><span style="font-family:Calibri;">   </span><span style="font-size:small;"><span style="font-family:Calibri;">// Do something</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">}</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;margin:0 0 0 .5in;"><span style="font-family:Symbol;"><span><span style="font-size:small;">·</span><span style="font-family:&quot;">         </span></span></span><span style="font-size:small;font-family:Calibri;">When checking out items make sure that your code can roll back changes or check the file back in when an exception occurs. Otherwise the item will remain in a checked out state and users will not be able to use that particular file. For example</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">try</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">{</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">   <span style="font-size:small;">file.CheckOut();</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">   <span style="font-size:small;">// Do something to the file</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">   <span style="font-size:small;">File.CheckIn(“comment”);</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">}</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">f<span style="font-size:small;">inally</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">{</span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">    <span style="font-size:small;">if (file.CheckOutStatus != SPCeckOutStatus.None)</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-family:Calibri;"><span style="font-size:small;">           <span style="font-size:small;">file.UndoCheckOut();</span></span></span></p>
<p class="MsoListParagraphCxSpMiddle" style="margin:0 0 0 .5in;"><span style="font-size:small;font-family:Calibri;">}</span></p>
<p class="MsoListParagraphCxSpLast" style="margin:0 0 6pt .5in;"> </p>
<p class="MsoListParagraphCxSpLast" style="margin:0 0 6pt .5in;"> </p>
<p class="MsoListParagraphCxSpLast" style="margin:0 0 6pt .5in;"><span style="font-size:small;font-family:Calibri;">Burlo</span></p>
<p class="MsoNormal" style="margin:6pt 0;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:6pt 0;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:6pt 0 6pt .25in;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<p class="MsoNormal" style="margin:6pt 0;"><span style="font-size:small;font-family:Calibri;"> </span></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sharepointexception.wordpress.com/4/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sharepointexception.wordpress.com/4/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sharepointexception.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sharepointexception.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sharepointexception.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sharepointexception.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sharepointexception.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sharepointexception.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sharepointexception.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sharepointexception.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sharepointexception.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sharepointexception.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sharepointexception.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sharepointexception.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sharepointexception.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sharepointexception.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sharepointexception.wordpress.com&amp;blog=4735714&amp;post=4&amp;subd=sharepointexception&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sharepointexception.wordpress.com/2008/09/14/bullet-proof-coding-sharepoint-error-checking-and-exception-handling/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/493ba0b8e3bdaf45f119083969702d4c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">burlo</media:title>
		</media:content>
	</item>
	</channel>
</rss>
