1.17. Exception Handling

That pretty much wraps up the WordCount program—at least in terms of its functionality. The primary remaining issue is that of error detection. For example, what if the user has specified a file that doesn't exist? Or what if the file is in a format we don't currently support? Checking that is simple. To find out if a file exists, we can invoke the Exists() function of the File class, passing to it the string supplied to us by the user:

using System.IO;
if ( ! File. Exists( file_name ))
      // oops ...

The harder part is choosing how to handle and/or report the problem. In the .NET environment, the convention is to report all program anomalies through exception handling. And that is what we will do.

Exception handling consists ...

Get C# Primer: A Practical Approach 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.