User-Defined Exceptions

Sometimes you need to report an error condition that doesn't map to an existing system-defined exception class. In such a case you can design a custom exception class for creating and throwing user-defined exceptions by creating a new class that inherits from the ApplicationException class.

Creating a custom exception class offers two benefits:

  • You can design exception objects that contain application-specific error information.

  • User-defined exception objects can be trapped and handled separately in their own independent Catch block.

Let's revisit the example from earlier in this chapter that involved the BankAccount class. Recall that the Withdraw method for a BankAccount object was written to throw an InvalidOperationException ...

Get Building Applications and Components with Visual Basic .NET 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.