Exception .data property in .net

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.  (

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.

string filename ...
try
{
   //... do something with the file
}
catch (FileNotFoundException ex)
{
   CustomException ex2 = new CustomException ("Missing cache: " + filename", ex);
   throw ex2;
}

This approach works but it leads to a lot of custom exceptions that are just extra work to create and maintain.  Sometimes you’ll want a custom exception because you are going to handle it in a different way in some outer scope, but often you cannot proceed and just want to log the error and redirect the user to an error page.  In these cases you can simplify things greatly using the .data property on an Exception.  This is an IDictionary for a “collection of key/value pairs that provide additional user-defined information about the exception”

Using this approach you can write:-

try
{
  ...
}
catch (FileNotFoundException ex)
{
   ex.Data.Add("cache filename", filename);
   throw;
}

Each surrounding scope can include a similar Try-Catch that adds more information to .Data so you get the full picture as to what might have caused the exception.
At a higher level in your Global.asax file where you catch all unhandled exceptions you might want to also go add to .Data for all the interesting parameters on HttpContext like RawUrl, cookies, …

ex.Data.Add("RawUrl", request.RawUrl);
try
{
   foreach (string cookieName in request.Cookies)
   {
      try
      {
         HttpCookie cookie = request.Cookies[cookieName];
         string key = "Cookie " + cookie.Path + " " + cookieName;
         if (!ex.Data.Contains(key))
           ex.Data.Add(key, cookie.Value.ToString());
      }
      catch
      {
         // deliberately nothing in here, should never happen, just being cautious
      }
   }
   if (request.IsABot())   // An extension method I use to spot bots - write your own ...
   {
      ex.Data.Add("BOT", "************* BOT *****************");
   }
   ex.Data.Add("UserAgent", request.UserAgent);
   ex.Data.Add("Referrer", request.UrlReferrer);
   ex.Data.Add("User Host", 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!
}

visit: Original Article

Advertisements
About

hi there, I am software engineer, working in e-commerce company and passionate about all thing digital. On this blog, I share my experiments with different technology.

Tagged with:
Posted in .Net, General

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What people say about post?
seo hizmeti on Awesome wordpress ecommerce…

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 38 other followers

%d bloggers like this: