I want to throw exception with additional data without creating exception variable. Something like this:
throw new Exception() { Data.Add("foo", "bar") };
Is it possible?
It looks like you should be able to do that with:
throw new Exception { Data = { { "foo", "bar" } } };
(Assuming you're using C# 3, which has collection initializers...)
Of course you can do this in conjunction with constructor arguments:
throw new ArgumentException("paramName") { Data = { { "foo", "bar" } } };
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