O'Reilly logo

C# Primer: A Practical Approach by Stanley B. Lippman

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

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 ...

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