User-Defined Exceptions

So far, this chapter has discussed only system-defined exception classes. However, C# also supports user-defined exception classes. These are exception classes that the users declare. This ensures that applications can be customized to produce application-specific error messages when exceptions are thrown during program execution.

There are a few restrictions on user-defined exception classes. These classes must declare the most commonly used constructors and the class name must end with the word “Exception.” It is also recommended that all user-defined exceptions be grouped under a separate namespace to enable better organization of all exception classes.

Listing 9.4 demonstrates a user-defined exception class.

Listing ...

Get Special Edition Using C# 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.