Exercise: Library with homemade exception

This is not about programming a real life library application - you are just supposed to have "fun" with exceptions.

Class Borrower

Program a simple class Borrower with properties like id, name, and address.

The following methods are important:

ReSharper (plugin for Visual Studio) can help you generate the methods: Type Alt + Insert, chose "Equality members"

This part of the exercise is not that important - do it quickly.

Make your own exception

Create your own checked exception class, LibraryException.

This class should have at least two constructors (both calling the base class constructor):

BorrowerCatalog.add with exception

Program a class BorrowerCatalog. Use a collection like List<Borrower> to hold the individual elements (Borrower objects in this case)

This time the add method must have the following signature

public void Add(Borrower borrower) 

The add methods throws exceptions if the borrower object is already present in the catalog.

Other methods, like delete, etc. are NOT interesting for this exercise.

Unit test to verify the add method

Make a Unit test to verify that the add method really throws an exception if you try to add the same borrower twice.

A little "test pattern" to test expected exceptions

try {     
  someMethodThatIsExpectedToThrowAnExeption();     
  Assert.Fail("This method should throw an exception"); // Test fails if we have no exception  
}  catch (TheException ex) { Assert.AreEqual("...", ex.Message); }

Note the comment in the catch block - never leave an exception catch block empty, as it will make your fellow programmer think that you ignored the exception by mistake.

Should BorrowerCatalog.Add really throw an exception?

Is it good or bad design to let the add method throw an exception? 
I.e. is add the same object twice really an exceptional condition?
Probably not. A better signature of add is

  public bool Add2(Borrower borrower)

Returns true if the borrower object is added, false otherwise.

Implement this alternative add2 method in the class BorrowerCatalog - and don't forget to test it.