Skip to content

Latest commit

 

History

History
27 lines (22 loc) · 1.64 KB

how-to-create-user-defined-exceptions.md

File metadata and controls

27 lines (22 loc) · 1.64 KB
titledescriptionms.datems.customdev_langshelpviewer_keywords
How to: Create User-Defined Exceptions
Learn how to create user-defined exceptions, which are an alternative to the hierarchy of exception classes derived from the Exception base class in .NET.
08/10/2022
devdivchpfy22
csharp
vb
user-defined exceptions
exceptions, examples
exceptions, user-defined

How to create user-defined exceptions

.NET provides a hierarchy of exception classes ultimately derived from the xref:System.Exception base class. However, if none of the predefined exceptions meet your needs, you can create your own exception class by deriving from the xref:System.Exception class.

When creating your own exceptions, end the class name of the user-defined exception with the word "Exception", and implement the three common constructors, as shown in the following example. The example defines a new exception class named EmployeeListNotFoundException. The class is derived from the xref:System.Exception base class and includes three constructors. [!code-csharpdg_exceptionDesign#14] [!code-vbdg_exceptionDesign#14]

Note

In situations where you're using remoting, you must ensure that the metadata for any user-defined exceptions is available at the server (callee) and to the client (the proxy object or caller). For more information, see Best practices for exceptions.

See also

close