<?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>Ian Mercer &#187; Exception</title>
	<atom:link href="http://blog.abodit.com/tag/exception/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.abodit.com</link>
	<description>Living in the World&#039;s Smartest House</description>
	<lastBuildDate>Sat, 07 Jan 2012 19:50:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Using Exception.Data to add additional information to an Exception</title>
		<link>http://blog.abodit.com/2010/03/using-exception-data-to-add-additional-information-to-an-exception/</link>
		<comments>http://blog.abodit.com/2010/03/using-exception-data-to-add-additional-information-to-an-exception/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 07:15:05 +0000</pubDate>
		<dc:creator>Ian Mercer</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Exception]]></category>

		<guid isPermaLink="false">http://blog.abodit.com/?p=507</guid>
		<description><![CDATA[Introduction Whether you are writing a WinForms application or a complex .NET web site, you will invariably be catching exceptions, logging them and reporting them somewhere. (In this post, I&#8217;m not going to explain how to log exceptions). Simply reporting the exception as-thrown rarely captures enough information to be able to diagnose what happened. A FileNotFoundException for <a href="http://blog.abodit.com/2010/03/using-exception-data-to-add-additional-information-to-an-exception/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>Whether you are writing a WinForms application or a complex .NET web  site, you will invariably be catching exceptions, logging them and  reporting them somewhere. (In this post, I&#8217;m not going to explain how  to log exceptions). Simply reporting the exception as-thrown rarely  captures enough information to be able to diagnose what happened. A <code>FileNotFoundException </code>for instance isn&#8217;t much use unless you know which file it was.</p>
<p>One way to deal with this issue is to wrap an exception up in a more  explicit exception that includes the extra information, e.g.</p>
<pre class="brush: csharp; title: ; notranslate">
string filename ...
try
{
   //... do something with the file
}
catch (FileNotFoundException ex)
{
   CustomException ex2 = new CustomException(&quot;Missing cache: &quot; + filename&quot;, ex);
   throw ex2;
}
</pre>
<p>This approach works but it leads to a lot of custom exceptions that  are just extra work to create and maintain.  Sometimes you&#8217;ll want a  custom exception because you are going to handle it in a different way  in some outer scope, but often you just want to log  the error and redirect the user to an error page as there is nothing else you can do to fix the problem.</p>
<p>In cases like this, you  can simplify things greatly by using the little-known <code>.data </code>property on an  Exception. This is an <code>IDictionary </code>for a &#8220;collection of  key/value pairs that provide additional user-defined information about  the exception&#8221; [MSDN].</p>
<p>Using this approach, you can write:</p>
<pre class="brush: csharp; title: ; notranslate">
try
{
   ...
}
catch (FileNotFoundException ex)
{
   ex.Data.Add(&quot;cache filename&quot;, filename);
   throw;
}
</pre>
<p>Each surrounding scope can include a similar <code>Try</code>-<code>Catch </code>that adds more information to <code>.Data </code>so by the time you get to the top-most scope you have added a complete picture as to what might have caused the exception.  And in doing so you haven&#8217;t lost any of the StackTrace information, nor have you wrapped the exception up needlessly in another exception.</p>
<p>At a higher level in your <em>Global.asax</em> file where you catch  all unhandled exceptions, you can add even more to the <code>.Data collection and perhaps include </code>all the interesting parameters on <code>HttpContext </code>like <code>RawUrl</code>,  cookies, &#8230;</p>
<pre class="brush: csharp; title: ; notranslate">

ex.Data.Add(&quot;RawUrl&quot;, request.RawUrl);
try
{
   foreach (string cookieName in request.Cookies)
   {
      try
      {
         HttpCookie cookie = request.Cookies[cookieName];
         string key = &quot;Cookie &quot; + cookie.Path + &quot; &quot; + cookieName;
         if (!ex.Data.Contains(key))
         {
             ex.Data.Add(key, cookie.Value.ToString());
         }
      }
      catch
      {
         // deliberately nothing in here, should
         // never happen, just being cautious
      }
   }
   // An extension method I use to spot bots - write your own ...
   if (request.IsABot())
   {
      ex.Data.Add(&quot;BOT&quot;, &quot;************* BOT *****************&quot;);
   }
   ex.Data.Add(&quot;UserAgent&quot;, request.UserAgent);
   ex.Data.Add(&quot;Referrer&quot;, request.UrlReferrer);
   ex.Data.Add(&quot;User Host&quot;, request.UserHostName);
}
catch
{
   // deliberately nothing in here, should
   // never happen, just being cautious
   // but we definitely don't want to cause
   // an exception while handling one!
}
</pre>
<h2>Exception Reporting Code</h2>
<p>Now in your exception reporting code, you can write out the exception  message and stack trace followed by a dump of all the key value pairs  in <code>.Data</code>. I tend to use log4net on each server writing to a  rolling log file and SQL server to capture the exception data  centrally. For SQL, you&#8217;ll probably want one table for the Exception  itself and another table with a row for each key/value pair in <code>.Data</code>.</p>
<h2>Comments</h2>
<p>One cause of Exceptions on web servers is bots and client-side &#8216;web  accelerators&#8217;.  Both of these can hit pages with incorrect or outdated  parameters that you simply didn&#8217;t expect to receive. That&#8217;s why I add a  BOT warning on every exception as the exception itself may seem severe  but in reality it&#8217;s benign and no user has ever seen it.  I even found one  antivirus product that takes each request you make and sends  the URL to Japan where another server makes a second request back to  check the page for viruses! It even pretends not to be a Bot in the  UserAgent and of course, all your &#8216;security- through-obscurity&#8217; URLs are  now sitting on a server in Japan, but you know security through  obscurity is no security at all, right?</p>
<p>Another browser add on called FunWebProducts would routinely corrupt  Viewstate information so if you see that in your exceptions log, you  know who to blame.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.abodit.com/2010/03/using-exception-data-to-add-additional-information-to-an-exception/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>System.Data.EntitySqlException</title>
		<link>http://blog.abodit.com/2009/05/system-data-entitysqlexception/</link>
		<comments>http://blog.abodit.com/2009/05/system-data-entitysqlexception/#comments</comments>
		<pubDate>Fri, 15 May 2009 03:04:00 +0000</pubDate>
		<dc:creator>Ian Mercer</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Exception]]></category>
		<category><![CDATA[fail]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://blog.abodit.com/?p=29</guid>
		<description><![CDATA[System.Data.EntitySqlException: &#8216;ExceptionRecord&#8217; could not be resolved in the current scope or context. This seemingly small Exception took a while to figure out.  I was calling the ObjectContext constructor without specifying the default container name.  Adding that solved the problem and now the Entity Framework is happily talking to SQL Server Compact again. For anyone else <a href="http://blog.abodit.com/2009/05/system-data-entitysqlexception/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<div><span class="Apple-style-span" style="border-collapse: collapse;"><span class="Apple-style-span" style="font-weight: bold;"><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><span class="Apple-style-span" style="font-size: large;">System.Data.</span></span></span><span class="Apple-style-span" style="font-weight: bold;"><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><span class="Apple-style-span" style="font-size: large;">EntitySqlException: &#8216;ExceptionRecord&#8217; could not be resolved in the current scope or context.</span></span></span></span><span class="Apple-style-span" style="font-weight: bold;"><span class="Apple-style-span" style="font-family: 'trebuchet ms';"><span class="Apple-style-span" style="font-size: large;"><br />
</span></span></span></div>
<div>This seemingly small Exception took a while to figure out.  I was calling the ObjectContext constructor without specifying the default container name.  Adding that solved the problem and now the Entity Framework is happily talking to SQL Server Compact again.</div>
<div>For anyone else looking for a solution to this, here&#8217;s the stack trace you might see:</div>
<div><span class="Apple-style-span" style="border-collapse: collapse; font-family: arial; font-size: 13px;"></p>
<div>
<div style="text-align: left;"><span class="Apple-style-span" style="font-family: georgia;"><span class="Apple-style-span" style="font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">System.Data.</span></span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">EntitySqlException: &#8216;ExceptionRecord&#8217; could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly., near escaped identifier.</span></span></div>
<p><span class="Apple-style-span" style="font-family: 'courier new';"> </span></p>
<div style="text-align: left;"><span class="Apple-style-span" style="font-family: arial;"><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;"> at System.Data.Common.EntitySql.</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">CqlErrorHelper.</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">ReportIdentifierError(Expr expr, SemanticResolver sr)</span></span></span></div>
<p><span class="Apple-style-span" style="font-family: 'courier new';"> </span></p>
<div style="text-align: left;"><span class="Apple-style-span" style="font-family: arial;"><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;"> at System.Data.Common.EntitySql.</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">SemanticAnalyzer.</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">ConvertIdentifier(Expr expr, SemanticResolver sr)</span></span></span></div>
<p><span class="Apple-style-span" style="font-family: 'courier new';"> </span></p>
<div style="text-align: left;"><span class="Apple-style-span" style="font-family: arial;"><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;"> at System.Data.Common.EntitySql.</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">SemanticAnalyzer.Convert(Expr astExpr, SemanticResolver sr)</span></span></span></div>
<p><span class="Apple-style-span" style="font-family: 'courier new';"> </span></p>
<div style="text-align: left;"><span class="Apple-style-span" style="font-family: arial;"><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;"> at System.Data.Common.EntitySql.</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">SemanticAnalyzer.</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">ConvertRootExpression(Expr astExpr, SemanticResolver sr)</span></span></span></div>
<p><span class="Apple-style-span" style="font-family: 'courier new';"> </span></p>
<div style="text-align: left;"><span class="Apple-style-span" style="font-family: arial;"><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;"> at System.Data.Common.EntitySql.</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">SemanticAnalyzer.Analyze(Expr astExpr, DbCommandTree commandTree)</span></span></span></div>
<p><span class="Apple-style-span" style="font-family: 'courier new';"> </span></p>
<div style="text-align: left;"><span class="Apple-style-span" style="font-family: arial;"><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;"> at System.Data.Common.EntitySql.</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">CqlQuery.AnalyzeSemantics(</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">DbCommandTree builderTree, Expr astExpr, Perspective perspective, ParserOptions parserOptions, Dictionary`2 parameters, Dictionary`2 variables)</span></span></span></div>
<p><span class="Apple-style-span" style="font-family: 'courier new';"> </span></p>
<div style="text-align: left;"><span class="Apple-style-span" style="font-family: arial;"><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;"> at System.Data.Common.EntitySql.</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">CqlQuery.Compile(DbCommandTree builderTree, String queryText, Perspective perspective, ParserOptions parserOptions, Dictionary`2 parameters, Dictionary`2 variables)</span></span></span></div>
<p><span class="Apple-style-span" style="font-family: 'courier new';"> </span></p>
<div style="text-align: left;"><span class="Apple-style-span" style="font-family: arial;"><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;"> at System.Data.Objects.</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">EntitySqlQueryState.Parse(</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">DbCommandTree parseTree)</span></span></span></div>
<p><span class="Apple-style-span" style="font-family: 'courier new';"> </span></p>
<div style="text-align: left;"><span class="Apple-style-span" style="font-family: arial;"><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;"> at System.Data.Objects.</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">EntitySqlQueryState.</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">GetExecutionPlan(Nullable`1 forMergeOption)</span></span></span></div>
<p><span class="Apple-style-span" style="font-family: 'courier new';"> </span></p>
<div style="text-align: left;"><span class="Apple-style-span" style="font-family: arial;"><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;"> at System.Data.Objects.</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">ObjectQuery`1.GetResults(</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">Nullable`1 forMergeOption)</span></span></span></div>
<p><span class="Apple-style-span" style="font-family: 'courier new';"> </span></p>
<div style="text-align: left;"><span class="Apple-style-span" style="font-family: arial;"><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;"> at System.Data.Objects.</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">ObjectQuery`1.System.</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">Collections.Generic.</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">IEnumerable</span></span><span class="Apple-style-span" style="font-family: 'courier new';"><span class="Apple-style-span" style="font-size: 10px;">.GetEnumerator()</span></span></span></div>
<ul>
<li></li>
</ul>
</div>
<p></span></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.abodit.com/2009/05/system-data-entitysqlexception/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Exception Handling using Exception.Data</title>
		<link>http://blog.abodit.com/2009/04/exception-data/</link>
		<comments>http://blog.abodit.com/2009/04/exception-data/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 21:38:00 +0000</pubDate>
		<dc:creator>Ian Mercer</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Exception]]></category>

		<guid isPermaLink="false">http://blog.abodit.com/?p=25</guid>
		<description><![CDATA[My latest article is up on Code Project: http://www.codeproject.com/KB/exception/ExceptionDotData.aspx]]></description>
			<content:encoded><![CDATA[<p>My latest article is up on Code Project: <a href="http://www.codeproject.com/KB/exception/ExceptionDotData.aspx">http://www.codeproject.com/KB/exception/ExceptionDotData.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.abodit.com/2009/04/exception-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript error reporting</title>
		<link>http://blog.abodit.com/2009/04/javascript-error-reporting/</link>
		<comments>http://blog.abodit.com/2009/04/javascript-error-reporting/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 21:08:00 +0000</pubDate>
		<dc:creator>Ian Mercer</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[Exception]]></category>
		<category><![CDATA[Test]]></category>

		<guid isPermaLink="false">http://blog.abodit.com/?p=24</guid>
		<description><![CDATA[It&#8217;s all very well to be capturing all the errors that happen server side, logging them to a database and reporting on them like any robust web service ought to, what about the client? So today I added reporting for client-side errors to the project I&#8217;m working on. Turned out to be remarkably easy. A <a href="http://blog.abodit.com/2009/04/javascript-error-reporting/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s all very well to be capturing all the errors that happen server side, logging them to a database and reporting on them like any robust web service ought to, what about the client?</p>
<p>So today I added reporting for client-side errors to the project I&#8217;m working on.  Turned out to be remarkably easy.  A couple of lines of JQuery code are all that&#8217;s needed on the client to capture the error and report it back to a web service:-</p>
<pre class="brush: jscript; title: ; notranslate">
window.onerror = errorHandler;

// Report javascript errors back to web site
function errorHandler(msg, url, lno) {
  try
  {
     jQuery.ajax({
        type: &quot;POST&quot;,
        url: '/services/error/clientError.ashx',
        processData: true,
        contentType: &quot;application/text&quot;,
        timeout: 10000,
        dataType: &quot;json&quot;,
        data: { msg: msg, url: url, lno: lno },
        success: function(responseData) {},
        error: function() {},
        complete: function() {}
     });
 }
 catch (err) {
     // do nothing
   }
 }
</pre>
<p>And on the server side the clientError.ashx can just grab the body of the post, split it back up and report on the error into the normal server side database of all exceptions.</p>
<p>In under an hour I added reporting for all client side errors and armed with that we&#8217;ll be able to find and fix those annoying browser (IE?) issues some of our customers see from time to time.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.abodit.com/2009/04/javascript-error-reporting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

