5. Exception Practices

Errors happen. Despite our best efforts, our programs encounter situations we didn’t anticipate. Throughout the .NET Framework, methods either succeed, or they indicate their failure by throwing an exception. The libraries and applications you write will be easier to use and extend if you follow this same practice. Making your code robust when exceptions are thrown is a key skill for every C# developer.

Your code will call methods that throw exceptions. Your code must behave in a well-known manner when it calls a method that throws an exception.

Your code may also throw exceptions directly. The .NET Framework Design Guidelines recommend throwing exceptions in all cases when your method does not do what was asked. You must ...

Get Effective C#: 50 Specific Ways to Improve Your C#, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.