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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.