O'Reilly logo

C# 5.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

Introducing Exceptions

With exceptions entering the picture, you can separate error handling from the regular flow of an application in a block-structured manner. For example, the following example illustrates the use of the System.IO .NET Framework APIs to read a file from disk:

IEnumerable<Customer> ReadCustomersFrom(string file) {    try {        using (FileStream fs = File.OpenRead(file)) {            using (StreamReader sr = new StreamReader(fs)) {                // Read from the file, creating Customer instances to return...            }        }    }    catch (IOException ex) {        // If we can handle this exception somehow, we can do it here.    }}

Various methods used in the preceding code can throw ...

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