Topic P - Exception Handling

Overview

The code for real-world programs is typically divided into distinct “layers” in order to make programs more flexible and maintainable. The layer that corresponds to the drivers we have seen so far is called the “Presentation Layer”. It’s called a “presentation layer” because its sole purpose is to interact with a user.

Almost all exception handling should take place in the driver, or presentation layer, of an application. Part of the reason for this is because the presentation layer is the “top” or “front-most” part of the program. When exceptions are handled at this layer, an application can report on the errors that occurred and give opportunity for the user to choose their desired response to the error.

LOGS

Code Samples

  1. DemoTestingForExceptions - This class is a very simple demonstration of how to catch exceptions and how exception handling allows a program to keep running even when a problem occurs.
  2. DemoBookFileAdapter - This class demonstrates displaying book information read from a file. It also demonstrates attempting to read from a file or directory that does not exist, and capturing an exception thrown by classes within the .Net Framework.
  3. (Not Available) MediaPlayer - This class demonstrates a complete working program that presents the user with a menu of various tasks related to loading and displaying songs on a CD. This class also demonstrates exception handling.

DemoTestingForExceptions

This class is a very simple demonstration of how to catch exceptions and how exception handling allows a program to keep running even when a problem occurs.

 1 public class DemoTestingForExceptions
 2 {
 3     private static ConsoleColor _Normal = Console.ForegroundColor;
 4 
 5     public static void Start()
 6     {
 7         string[] actualAuthors = { "Dan Gilleland", "B. N. D'erdundat", "Dr. Ink\
 8 " };
 9         string actualTitle = "Java: Bean There, Done That";
10         string actualBarCode = "0-12345678-9";
11 
12         // Demo a normal creation of a Book object
13         Console.WriteLine("Testing valid data");
14         TestBook(actualTitle, actualAuthors, actualBarCode);
15 
16         // Demo testing the Book class for various combinations of data
17         Console.WriteLine("Testing empty title");
18         TestBook("", actualAuthors, actualBarCode);
19         Console.WriteLine("Testing null title");
20         TestBook(null, actualAuthors, actualBarCode);
21         Console.WriteLine("Testing empty author list");
22         TestBook(actualTitle, new String[0], actualBarCode);
23         Console.WriteLine("Testing null author list");
24         TestBook(actualTitle, null, actualBarCode);
25         Console.WriteLine("Testing empty bar code");
26         TestBook(actualTitle, actualAuthors, "");
27         Console.WriteLine("Testing null bar code");
28         TestBook(actualTitle, actualAuthors, null);
29         Console.WriteLine("Testing all nulls");
30         TestBook(null, null, null);
31     }
32 
33     private static void TestBook(string title, string[] authors, string barCode)
34     {
35         try
36         {
37             Book theBook = new Book(title, authors, new ISBN(barCode));
38             Console.WriteLine("I creatd the following book:");
39             Console.WriteLine("    Title: " + theBook.Title);
40             Console.Write("    Authors: ");
41             foreach (string author in theBook.Authors)
42                 Console.Write(author + ", ");
43             Console.WriteLine("(" + theBook.Authors.Length + " authors)");
44             Console.WriteLine("    ISBN: " + theBook.Isbn.BarCode);
45             Console.WriteLine("-----------------------------------------");
46             Console.WriteLine();
47         }
48         catch (Exception ex)
49         {
50             Console.ForegroundColor = ConsoleColor.Red;
51             Console.WriteLine("!-- " + ex.Message + " --!");
52             Console.ForegroundColor = _Normal;
53         }
54     }
55 }

DemoBookFileAdapter

This class demonstrates displaying book information read from a file. It also demonstrates attempting to read from a file or directory that does not exist, and capturing an exception thrown by classes within the .Net Framework.

 1 public class DemoBookFileAdapter
 2 {
 3     private static ConsoleColor _Normal = Console.ForegroundColor;
 4 
 5     public static void Start()
 6     {
 7         try
 8         {
 9             Book[] myBooks;
10             myBooks = BookFileAdapter.LoadList("BookList.txt", FileFormat.CSV).T\
11 oArray();
12             DisplayBooks(myBooks);
13 
14             myBooks = BookFileAdapter.LoadList(@"V:\ThisDriveNameDoesNotExist", \
15 FileFormat.CSV).ToArray();
16             myBooks = BookFileAdapter.LoadList("ThisFileNameDoesNotExist", FileF\
17 ormat.CSV).ToArray();
18 
19             DisplayBooks(myBooks);
20         }
21         catch (DirectoryNotFoundException ex)
22         {
23             Console.ForegroundColor = ConsoleColor.Red;
24             Console.WriteLine("!-- " + ex.Message + " --!");
25             Console.ForegroundColor = _Normal;
26         }
27         catch (FileNotFoundException ex)
28         {
29             Console.ForegroundColor = ConsoleColor.Red;
30             Console.WriteLine("!-- " + ex.Message + " --!");
31             Console.ForegroundColor = _Normal;
32         }
33         catch (Exception ex)
34         {
35             Console.ForegroundColor = ConsoleColor.Red;
36             Console.WriteLine("!-- " + ex.Message + " --!");
37             Console.ForegroundColor = _Normal;
38         }
39     }
40 
41     private static void DisplayBooks(Book[] myBooks)
42     {
43         for (int index = 0; index < myBooks.Length; index++)
44         {
45             Console.WriteLine("Title  : " + myBooks[index].Title);
46             Console.WriteLine("ISBN   : " + myBooks[index].Isbn.BarCode);
47             Console.WriteLine("Authors: ");
48             foreach (string author in myBooks[index].Authors)
49                 Console.WriteLine("\t" + author);
50             Console.WriteLine();
51         }
52     }
53 }

MediaPlayer

This class demonstrates a complete working program that presents the user with a menu of various tasks related to loading and displaying songs on a CD. This class also demonstrates exception handling.

Practice Exercises

  1. Testing Suite - Using any two classes from previous topics, create a program that tests the construction of objects with complete exception handling.
  2. Working Program - With the help of the UserPrompt class, create a simple program involving any classes from previous topics. Be sure to include exception handling in your program.

Testing Suite

Using any two classes from previous topics, create a program that tests the construction of objects with complete exception handling.

Working Program

With the help of the UserPrompt class, create a simple program involving any classes from previous topics. Be sure to include exception handling in your program.