开发者

In caught exception -> e.Data.Clear(); // what is this supposed to do?

I've seen something like this too many times:

try
{
    //something
}
catch (FileNotFoundException e)
{
    e.Data.Clear(); // <- WTF?
}

and as far as I know this is useless voodoo, but please correct me开发者_运维百科 if I'm wrong.


Exceptions in .Net allow you to associate arbitrary data with them by putting it into the Data member of Exception. It's essentially a weakly typed dictionary which makes minimal checks to ensure the stored information is serializable.

This particular function is simply clearing out all of the custom associated data. Why is a bit of a mystery since the developer left no comments.

As to whether or not this is useless. In all likely hood yes. The one specific case where this could be useful is if the creator of the FileNotFoundException both added an object reference to the Data bag and is itself holding onto a reference of the FileNotFoundException instance. In that specific case calling Clear could prevent a memory leak by removing a reference to an object which should be short lived. I find it very unlikely that this is the case though. My instinct would be to file a bug / delete it .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜