.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

  • Semantic Kernel Agent Framework Graduates to Release Candidate

    With agentic AI now firmly established as a key component of modern software development, Microsoft graduated its Semantic Kernel Agent Framework to Release Candidate 1 status.

  • TypeScript 5.8 Improves Type Checking, Conditional Feature Delayed to 5.9

    Microsoft shipped TypeScript 5.8 with improved type checking in some scenarios, but thorny problems caused the dev team to delay related work to the next release.

  • Poisson Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demo of Poisson regression, where the goal is to predict a count of things arriving, such as the number of telephone calls received in a 10-minute interval at a call center. When your source data is close to mathematically Poisson distributed, Poisson regression is simple and effective.

  • Cloud-Focused .NET Aspire 9.1 Released

    Along with .NET 10 Preview 1, Microsoft released.NET Aspire 9.1, the latest update to its opinionated, cloud-ready stack for building resilient, observable, and configurable cloud-native applications with .NET.

  • Microsoft Ships First .NET 10 Preview

    Microsoft shipped .NET 10 Preview 1, introducing a raft of improvements and fixes across performance, libraries, and the developer experience.

Subscribe on YouTube

Upcoming Training Events