Monday, March 29, 2010

What order should exceptions be caught in, least specific to most specific or vice versa?

Exceptions within the .NET frame work should be caught from most specific to least specific. This is because only the first catch block with a matching exception type is the only block that is executed. So in other words if you catch a more general exception before a less general exception, the code will never make it beyond the general exception that is caught. The more specific catch block becomes unreachable code.

Additional Resources
.NET Exception Handling (Microsoft)

No comments:

Post a Comment