Problem adding Exception.Data to dictionary
I am trying to add a key value pair and cannot add a key to the Exception.Data:
The enumeration is of type int (default)
catch (Exception ex)
{
ex.Data.Add(Enums.ExceptionData.SomeName, _someText);
}
note: when I add a clock for Enums.ExceptionData.SomeName, I get SomeName, rename name back. for the line above when trying to add this as a key to a dictionary.
When I try to check ex.Data further down the stack, it returns null. This is how I try to test it:
ex.Data[Enums.ExceptionData.SomeName].ToString()
So, this is how it all goes. First, in my Request.cs Abstract class, this code ends up running (yes, _someText has a valid string):
try
{
// Send the Request
requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
// get response
response = (HttpWebResponse)request.GetResponse();
}
catch (Exception ex)
{
// include SOAP string that was sent
ex.Data.Add(Enums.ExceptionDataRequest.SomeName, _someText);
string test;
}
In my codebase, I am calling this method :
try
{
radio.UpdateFrequency(...);
LogFrequency();
}
catch (Exception ex)
{
radio.LogFailure(..., ex.Data[Enums.ExceptionDataRequest.SomeName].ToString());
}
and this is what radio.UpdateFrequency looks like:
public void UpdateFrequency(...)
{
....
// update frequency (which also performs opt-in)
FrequencyRequest request = new FrequencyRequest(actionID, email, listID);
FrequencyResponse response = (FrequencyResponse)request.SendRequest();
....
}
so if that doesn't work, (at least trust me) the request error pushes its way before my try / catch in my code:
FrequencyRequest request = new FrequencyRequest(actionID, email, listID);
fails, now grab this data in my try-catch in my code.
source to share
You are adding stuff to a dictionary using the enum value as a key and querying it with a string key (not an enum). Modify the above request code as follows and it should work fine.
ex.Data[Enums.ExceptionData.SomeName].ToString()
This code example writes hello world
to the console. Is there an _someText
empty string in your example?
namespace ConsoleApplication1
{
using System;
enum Values
{
Value1
}
class Program
{
static void Test()
{
try
{
int a = 0;
int c = 12 / a;
}
catch (Exception ex)
{
ex.Data.Add(Values.Value1, "hello world");
throw ex;
}
}
static void Main(string[] args)
{
try
{
Test();
}
catch (Exception ex)
{
Console.WriteLine(ex.Data[Values.Value1].ToString());
}
Console.ReadLine();
}
}
}
source to share