开发者

order of exception catching?

lets say I have a System exception like UnauthorizedAcce开发者_运维百科ssException and an exception which I have written myself. Is there a certain order that I have to use whn catching the exceptions? I guess its still from the most specific to the least specific?

Thanks :)


They are caught in the order as-written, so put the most specific (in terms of inheritance between exception types) first.

Since it is the type that matters (and the inheritance hierarchy); if the two don't have an inheritance relationship (i.e. it is not the case that YourFunkyException inherits from UnauthorizedAccessException directly or indirectly), then it won't matter.

  • "Design Guidelines for Exceptions" at http://msdn.microsoft.com/en-us/library/ms229014.aspx
  • "Handling and Throwing Exceptions" at http://msdn.microsoft.com/en-us/library/5b2yeyab.aspx.


Yes, first of all catch "custom" exception like yours and have the catch (Exception ex) last to catch all other types.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜