Inheriting and extending .NET types
.NET has prebuilt class libraries containing hundreds of thousands of types. Rather than creating your own completely new types, you can often start by inheriting from one of Microsoft's.
Inheriting from an exception
In the PacktLibrary
project, add a new class named PersonException
, as shown in the following code:
using System; namespace Packt.CS7 { public class PersonException : Exception { public PersonException() : base() { } public PersonException(string message) : base(message) { } public PersonException(string message, Exception innerException) : base(message, innerException) { } } }
Note
Unlike ordinary methods, constructors are not inherited, so we must explicitly declare and explicitly call the base constructor implementations in System.Exception
to make them available to programmers who might want to use those constructors in our custom exception.
In the Person
class, add the following method:
public void...