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