In C#, will the folloing code throw e
containing the additional information up the call stack?
...
catch(Exception e)
{
e.Data.Add("Additional information","blah blah");
throw;
}
Yes, it will. A lot of developers don't realise that the following code will throw a new exception from that point in the call-stack, not the calls made previously up the stack before the catch
.
...
catch(Exception e)
{
e.Data.Add("Additional information","blah blah");
throw e;
}
I learnt this the hard way!
var answer = "No";
try
{
try
{
throw new Exception();
}
catch (Exception e)
{
e.Data.Add("mykey", "myvalue");
throw;
}
}
catch (Exception e)
{
if((string)e.Data["mykey"] == "myvalue")
answer = "Yes";
}
Console.WriteLine(answer);
Console.ReadLine();
When you run the code you will find that the answer is yes :-)
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