I am coding a MVC 5 internet application, and I have a question in regards to throwing exceptions.
How is the best way to include object data when throwing an exception. I am wanting the exception to display specific object data when an email is sent to me when an exception occurs.
Some options I found:
public virtual IDictionary Data
property. I could manually add data to the Data
property.Is there a recommended way to do this?
I would definitely go for the Data
dictionary, since that is the closest to the actual exception. Also, it doesn't need access to the file system, etc. to work.
We use the Data
dictionary ourselves a lot, and it helps to have a list of keys you, so you can easily reference the data again.
For example:
public const string SOME_KEY = "some_key";
Exception e = new Exception("some error");
e.Data.Add(SOME_KEY, someValue);
throw e;
Re-use it later:
object some_key = e.Data[SOME_KEY];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With