There's a Data
property on the Exception
class. You could add your data to that.
It implements IDictionary
... just add your key/value pairs to it, like this:
var ex = new Exception(string.Format("{0} - {1}", statusMessage, statusCode));
ex.Data.Add(statusCode, statusMessage); // store "3" and "Invalid Parameters"
throw ex;
Then read it back out in your catch
block. The Key
and Value
are both of type object
, so you'll have to convert them back to their original types.
catch (Exception exc)
{
var statusCode = exc.Data.Keys.Cast<string>().Single(); // retrieves "3"
var statusMessage = exc.Data[statusCode].ToString(); // retrieves "Invalid Parameters"
}