Writing Custom Exceptions

The .NET Framework's built-in exception classes will accommodate most abnormal conditions you encounter. However, when you need additional capabilities, you can write your own custom-made exception classes by using System.ApplicationException as a base class.

Lines 3–26 of Listing 19.7 illustrate this possibility by defining a new exception class called LogarithmicException.

Listing 19.7. CustomException.cs
 01: using System; 02: 03: public class LogarithmicException : System.ApplicationException 04: { 05: private uint errorNumber; 06: 07: 08: public LogarithmicException() : base("Logarithmic exception") 09: { 10: errorNumber = 1000; 11: } 12: 13: 14: public LogarithmicException(string message, uint initErrorNumber) ...

Get C# Primer Plus 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.