【发布时间】:2012-11-21 06:24:02
【问题描述】:
我知道异常捕获可能会很昂贵,但我想知道是否存在实际上比查找更便宜的情况?
例如,如果我有一个大字典,我可以测试是否存在一个键:
If MyDictionary.ContainsKey(MyKey) Then _
MyValue = MyDictionary(MyKey) ' This is 2 lookups just to get the value.
或者,我可以捕获一个异常:
Try
MyValue = MyDictionary(MyKey) ' Only doing 1 lookup now.
Catch(e As Exception)
' Didn't find it.
End Try
异常捕获总是比上述查找更昂贵,还是在某些情况下更便宜?
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。