<?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/"
	>

<channel>
	<title>.Net Smoothie &#187; Code Snippets</title>
	<atom:link href="http://www.richardbushnell.net/tag/code-snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.richardbushnell.net</link>
	<description></description>
	<lastBuildDate>Thu, 12 Mar 2009 13:52:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Writing Custom Exception Classes the Quick Way</title>
		<link>http://www.richardbushnell.net/2008/02/15/writing-custom-exception-classes-the-quick-way/</link>
		<comments>http://www.richardbushnell.net/2008/02/15/writing-custom-exception-classes-the-quick-way/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 16:00:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C# 2.0]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/02/15/writing-custom-exception-classes-the-quick-way/</guid>
		<description><![CDATA[Until recently I thought this was a well-known feature. After demonstrating it a few times, I found out it wasn&#8217;t. A long time ago, in an cubicle far, far away, someone created the .Net Framework. To cut a long story short, they simultaneously produced guidelines for creating Exception classes, which you should always use or [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F15%2Fwriting-custom-exception-classes-the-quick-way%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Until recently I thought this was a well-known feature. After demonstrating it a few times, I found out it wasn&#8217;t.</p>
<p>A long time ago, in an cubicle far, far away, someone created the .Net Framework. To cut a long story short, they simultaneously produced guidelines for creating Exception classes, which you should always use or face having your fingernails pulled out with a staple-gun.</p>
<p>The guidelines state:</p>
<blockquote>
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Arial','sans-serif'">&#8220;Use the common constructors shown in the following code example when creating exception classes. &#8220;<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'">[C#]<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New'">public</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">class</span> XxxException : ApplicationException<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'">{<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'"><span>   </span><span style="color: blue">public</span> XxxException() {&#8230; }<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'"><span>   </span><span style="color: blue">public</span> XxxException(<span style="color: blue">string</span> message) {&#8230; }<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'"><span>   </span><span style="color: blue">public</span> XxxException(<span style="color: blue">string</span> message, Exception inner) {&#8230; }<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'"><span>   </span><span style="color: blue">public</span> XxxException(SerializationInfo info, StreamingContext context) {&#8230;}<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'">}<o:p></o:p></span></p></blockquote>
<p><span id="more-53"></span><br />
If you don&#8217;t believe me, you can look here:</p>
<p><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp" title="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp</a></p>
<p>Now, I don&#8217;t know about you, but I can&#8217;t remember all that every time I create a custom exception class. Maybe that&#8217;s why the guidelines also state:</p>
<blockquote><p>In most cases, use the predefined exception types. Only define new exception types for programmatic scenarios, where you expect users of your class library to catch exceptions of this new type and perform a programmatic action based on the exception type itself.</p></blockquote>
<p>But let&#8217;s ignore that. It&#8217;s not relevant.</p>
<p>So, <em>did you know</em> that there is a Visual Studio code snippet that does the work for you? Here&#8217;s how you use it.</p>
<ol>
<li>Create a new class.</li>
<li>inside the namespace definition of the new class, type
<pre class="code">Exception</pre>
<p><a href="http://11011.net/software/vspaste"></a></li>
<li>Press the tab key twice.</li>
</ol>
<p>You then get the following code, free of charge:</p>
<pre class="code">[<span style="color: blue">global</span>::System.<span style="color: #2b91af">Serializable</span>]
<span style="color: blue">public class </span><span style="color: #2b91af">MyException </span>: <span style="color: #2b91af">Exception </span>{
<span style="color: green">
  </span><span style="color: blue">public </span>MyException() { }
  <span style="color: blue">public </span>MyException(<span style="color: blue">string </span>message) : <span style="color: blue">base</span>(message) { }
  <span style="color: blue">public </span>MyException(<span style="color: blue">string </span>message, <span style="color: #2b91af">Exception </span>inner) : <span style="color: blue">base</span>(message, inner) { }
  <span style="color: blue">protected </span>MyException(
  System.Runtime.Serialization.<span style="color: #2b91af">SerializationInfo </span>info,
  System.Runtime.Serialization.<span style="color: #2b91af">StreamingContext </span>context)
    : <span style="color: blue">base</span>(info, context) { }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>What&#8217;s more, you get code snippet functionality which automatically changes the names of all the constructors when you change the name of the class.</p>
<p>I wonder what other code snippets are available. Do you know of any cool ones? What are your favorites?</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F15%2Fwriting-custom-exception-classes-the-quick-way%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/02/15/writing-custom-exception-classes-the-quick-way/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
