.NET Tips and Tricks

Blog archive

Add Custom Data to Exceptions

When it comes to debugging problems with your code, a good Exception object is your most valuable tool. I've talked before about how why returning information about an exception using the InnerException object will let you find out what the real problem is without giving away secrets about your application.

I've also discussed how to ensure that your server-side errors aren't masked by the generic 500 "something's gone wrong" HTTP error. I've even gone as far as to suggest that it might be useful to create your own custom exception object.

Some people might conclude that my programs must throw a lot of errors but that's not true. My concern is simple: Knowing precisely what went wrong when your program blows up helps you figure out what the problem is. Furthermore, knowing what data exposed the problem is often critical to solving the problem. To help you out with that, the Exception object provides a dictionary where you can tuck away data about an error that programs processing the error can use -- it's the Exception object's Data property.

The following code adds an item to an Exception object's Data collection with a key of CustomerId and sets that key to a value. The code then throws an Exception of its own, tucking the original exception (with its additional information) into the InnerException property:

Try
   ...code that might throw an exception...
Catch ex As Exception
   ex.Data.Add("CustomerId", custId)
   Throw New Exception("Failure in processing Customer", ex)
End Try

Alternatively, you can create your own Exception object and add to its Data collection:

Try
   ...code that might throw an exception...
Catch ex As Exception
   Dim myEx As New Exception("Failure in processing Customer", ex)
   myEx.Data.Add("PersonId", pers.FirstName)
   Throw myEx
End Try

The code that processes an Exception doesn't have to know precisely what's in the Data collection in order to report on it. Instead, you can just write out everything in the Data collection using code like this:

For Each key In ex.Data.Keys
  Debug.Print(key & ": " & ex.Data(key))
Next

There are two things to avoid here. First, make sure that retrieving the information you're going to put in the Data collection won't itself trigger an exception (don't try to set a key to the property of a potentially null reference, for example). Second, don't add to the collection of any Exception object that you haven't just created because you might overwrite a Data value that was set elsewhere.

Posted by Peter Vogel on 02/23/2016


comments powered by Disqus

Featured

  • IDE Irony: Coding Errors Cause 'Critical' Vulnerability in Visual Studio

    In a larger-than-normal Patch Tuesday, Microsoft warned of a "critical" vulnerability in Visual Studio that should be fixed immediately if automatic patching isn't enabled, ironically caused by coding errors.

  • Building Blazor Applications

    A trio of Blazor experts will conduct a full-day workshop for devs to learn everything about the tech a a March developer conference in Las Vegas keynoted by Microsoft execs and featuring many Microsoft devs.

  • Gradient Boosting Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the gradient boosting regression technique, where the goal is to predict a single numeric value. Compared to existing library implementations of gradient boosting regression, a from-scratch implementation allows much easier customization and integration with other .NET systems.

  • Microsoft Execs to Tackle AI and Cloud in Dev Conference Keynotes

    AI unsurprisingly is all over keynotes that Microsoft execs will helm to kick off the Visual Studio Live! developer conference in Las Vegas, March 10-14, which the company described as "a must-attend event."

  • Copilot Agentic AI Dev Environment Opens Up to All

    Microsoft removed waitlist restrictions for some of its most advanced GenAI tech, Copilot Workspace, recently made available as a technical preview.

Subscribe on YouTube