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 .
精彩评论