A.1. Why Exception Handling?

If you’ve been writing software for very long, you probably already know why you want to handle exceptions. Have you ever seen a program crash and display a weird message that doesn’t make any sense? This is what happens when developers do not handle exceptions properly. Let’s look at a simple example. Listing A.1 opens a file named c:\abc.txt.

Listing A.1. Opening a file
using System;
using System.IO;

namespace ListingA1andA2
{
  class Class1
  {
    static void Main(string[] args)
    {
      File.Open("c:\\abc.txt", FileMode.Open);
    }
  }
}

What if the file does not exist? We get the error message shown in Figure A.1. We are fortunate that CLR handles so much for us because otherwise this error message could have been a lot worse. ...

Get Graphics Programming with GDI+ 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.