A bit longer code BUT a lot shorter to debug (using the logs)
Today I got a phone call from our support tier describing me a failed use case and providing me with the exception from our log.
The exception looked something like this (faked):
In first look of it I couldn't understand what is the failing area (because I remember that the class uses more than one dictionary) :(
The class that the exception occurs in looks like this:
As you can see the method uses two dictionaries, so without looking at the source (and line of exception) I cant say what is the problematic argument / dictionary.
I have created a new method named "Add_2" which is a simple refactor of the code that I have wrapped every operation on the dictionary with try and catch and a simple new argument exception that gives a context to the problem when it occurs.
So running the following code:
Will give the following exception:
So now I know the exact failing dictionary and key without looking at the code and you know what even better the support tier can know this also without contacting me (and who knows maybe they will fix it :) )
- Method "Add_2" is only a simple implementation and its only for explanation purposes (there are other methods to add context to the exception).
- Make sure that end user doesn't see your exceptions as this is a security issues (in my case the exceptions are logged)
- You can download the code also here
I think Dictionary class should have done a better job and describe the exception better, Don you?