.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

  • Random Neighborhoods Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the random neighborhoods regression technique, where the goal is to predict a single numeric value. Compared to other ML regression techniques, advantages are that it can handle both large and small datasets, and the results are highly interpretable.

  • As Some Orgs Restrict DeepSeek AI Usage, Microsoft Offers Models and Dev Guidance

    While some organizations are restricting employee usage of the new open source DeepSeek AI from a Chinese company due to data collection concerns, Microsoft has taken a different approach.

  • Useful New-ish Features in .NET/C#

    We often hear about the big new features in .NET or C#, but what about all of those lesser known, but useful new features? How exactly do you use constructs like collection indices and ranges, date features, and pattern matching?

  • TypeScript 5.8 Beta Speeds Program Loads, Updates

    "TypeScript 5.8 introduces a number of optimizations that can both improve the time to build up a program, and also to update a program based on a file change in either --watch mode or editor scenarios."

  • AI Toolkit for VS Code Now Lets You 'Bring Your Own Model'

    "AI Toolkit extension for VS code now supports external local models via Ollama. It has also added support remote hosted models using API keys for OpenAI, Google and Anthropic."

Subscribe on YouTube