views:

147

answers:

1

I am trying to add a key value pair and having trouble adding the key to Exception.Data:

The enum is of type int (default)

        catch (Exception ex)
        {
            ex.Data.Add(Enums.ExceptionData.SomeName, _someText);
        }

note: when I add a watch for Enums.ExceptionData.SomeName, I get SomeName, the name of the enum back. for the line above when trying to add that as a key to the dictionary.

When I try to check the ex.Data further up the stack, it's returning null. Here's how I attempt to check it:

ex.Data[Enums.ExceptionData.SomeName].ToString()

So here's how it all goes down. First, in my Request.cs Abstract class, this code eventually runs (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 code-behind I call this method:

        try
        {
            radio.UpdateFrequency(...);
            LogFrequency();
        }
        catch (Exception ex)
        {
            radio.LogFailure(..., ex.Data[Enums.ExceptionDataRequest.SomeName].ToString());
        }

and here's how radio.UpdateFrequency looks:

    public void UpdateFrequency(...)
    {
        ....

        // update frequency (which also performs opt-in)
        FrequencyRequest request = new FrequencyRequest(actionID, email, listID);
        FrequencyResponse response = (FrequencyResponse)request.SendRequest();

        ....
    }

so if this fails, (at least believe) the request error bubbles up to my try/catch in my code-behind:

FrequencyRequest request = new FrequencyRequest(actionID, email, listID);

fails, now grab that data in my try-catch in my code-behind.

+2  A: 

You are adding stuff to the dictionary using the enum value as key and querying it with a string key (not enum). Change the above query code as follows and it should work just fine.

ex.Data[Enums.ExceptionData.SomeName].ToString()


This sample code writes hello world in the console. Is _someText in your example a null string?

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();
        }
    }
}
smink
Yea, did that but I get a null ref exception still when trying to retrieve.
CoffeeAddict
'ex.Data[Enums.ExceptionData.SomeName]' is null string
CoffeeAddict
And yes, _someText has a valid string value
CoffeeAddict
But what if I catch it (but not throw) somewhere in between where I'm adding this data and trying to catch it again, would that data be removed up the stack for some reason if you catch it once already and do something?
CoffeeAddict
weird, for some reason I've done exactly the same way you have and I still get null when it tries to find that key
CoffeeAddict
I even tried hard coding in the key value string
CoffeeAddict
I've updated the original thread with more information...
CoffeeAddict
OK, I had to rethrow the error. Data is preserved. I guess you have to rethrow to preserve any exception data in that data dictionary.
CoffeeAddict
I still do not get why I had to rethrow
CoffeeAddict
@coffeaddict: If the exception is not rethrown it gets swallowed.
Alfred Myers
@smink: "throw" will preserve the stack trace and "throw ex" will not. Because of that, generally, "throw" is a better option than "throw ex"
Alfred Myers