.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

  • VS Code Copilot Previews New GPT-4o AI Code Completion Model

    The 4o upgrade includes additional training on more than 275,000 high-quality public repositories in over 30 popular programming languages, said Microsoft-owned GitHub, which created the original "AI pair programmer" years ago.

  • Microsoft's Rust Embrace Continues with Azure SDK Beta

    "Rust's strong type system and ownership model help prevent common programming errors such as null pointer dereferencing and buffer overflows, leading to more secure and stable code."

  • Xcode IDE from Microsoft Archrival Apple Gets Copilot AI

    Just after expanding the reach of its Copilot AI coding assistant to the open-source Eclipse IDE, Microsoft showcased how it's going even further, providing details about a preview version for the Xcode IDE from archrival Apple.

  • Introduction to .NET Aspire

    Two Microsoft experts will present on the cloud-native application stack designed to simplify the development of distributed systems in .NET at the Visual Studio Live! developer conference coming to Las Vegas next month.

  • Microsoft Previews Copilot AI for Open-Source Eclipse IDE

    Catering to Java jockeys, Microsoft is yet again expanding the sprawling reach of its Copilot-branded AI assistants, previewing a coding tool for the open-source Eclipse IDE.

Subscribe on YouTube

Upcoming Training Events