O'Reilly logo

C# 4.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

When and What to Catch

Handling exceptions correctly is a huge responsibility for framework and application developers. For that reason alone, it’s important to give some guidelines on when and what to catch.

Let’s start with the latter question: what exceptions to catch. You first want to know which exceptions can be thrown by the APIs being used; for this information, documentation proves invaluable. As an example, consider the System.IO.File.OpenRead API, where the following exceptions are documented:

ArgumentException—The path is a zero-length string, contains only whitespace or contains one or more invalid characters as defined by InvalidPathChars.

ArgumentNullException—The path is null.

PathTooLongException—The specified path, filename, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required