Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

How to create user-defined exceptions

  • 2022-08-12
Feedback

In this article

.NET provides a hierarchy of exception classes ultimately derived from theException base class. However, if none of the predefined exceptions meet your needs, you can create your own exception class by deriving from theException 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 namedEmployeeListNotFoundException. The class is derived from theException base class and includes three constructors.

using System;public class EmployeeListNotFoundException : Exception{    public EmployeeListNotFoundException()    {    }    public EmployeeListNotFoundException(string message)        : base(message)    {    }    public EmployeeListNotFoundException(string message, Exception inner)        : base(message, inner)    {    }}
Public Class EmployeeListNotFoundException    Inherits Exception    Public Sub New()    End Sub    Public Sub New(message As String)        MyBase.New(message)    End Sub    Public Sub New(message As String, inner As Exception)        MyBase.New(message, inner)    End SubEnd Class

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, seeBest practices for exceptions.

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo