CLR via C # Note: custom exception

Source: Internet
Author: User

Exception types derived from exception should be serializable, because only in this way can the exception object be in cross-applicationProgramDomain.

 

[Serializable]
Public   Sealed   Class Diskfullexception: exception, iserializable
{
Private   String M_diskpath;
Public   String Diskpath { Get { Return M_diskpath ;}}

Public   Override   String Message
{
Get
{
If (M_diskpath =   Null ) Return   Base . Message;
Stringbuilder msg =   New Stringbuilder ( Base . Message );
MSG. appendformat ( " (Diskpath = {0}) {1} " , M_diskpath, environment. newline );
Return MSG. tostring ();
}
}

Public Diskfullexception (): Base (){}
Public Diskfullexception ( String Message ): Base (Message ){}
Public Diskfullexception ( String Message, exception innerexception)
: Base (Message, innerexception)
{}

Public Diskfullexception ( String Message, String Diskpath)
: This (Message)
{
M_diskpath = Diskpath;
}
Public Diskfullexception ( String Message, String Diskpath, exception innerexception)
: This (Message, innerexception)
{
M_diskpath = Diskpath;
}

// Defines a deserialization constructor. Because the class is sealed, the constructor is private;
// Otherwise, the constructor can be protected.
Private Diskfullexception (serializationinfo info, streamingcontext context)
: Base (Info, context)
{
// Deserialization of custom Fields
M_diskpath = Info. getstring ( " Diskpath " );
}

# Region Iserializable Member
// Make sure that the caller obtains the internal status of the object.
[Securitypermission (securityaction. Demand, serializationformatter =   True )]
Void Iserializable. getobjectdata (serializationinfo info, streamingcontext context)
{
Base . Getobjectdata (Info, context );
// Serialize custom Fields
Info. addvalue ( " Diskpath " , M_diskpath );
}
# Endregion
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.