Code specification custom exception

Source: Internet
Author: User

As mentioned in many standards, custom Exceptions require a unified constructor. For example, the following four constructor functions are implemented by default.

[Serializable]

Public   Class Xxxexception: applicationexception

{

Public Xxxexception (): Base ()

{



}


Public Xxxexception ( String Message ): Base (Message)

{



}


Public Xxxexception ( String Message, exception innerexception ): Base (Message, innerexception)

{


}


Protected Xxxexception (system. runtime. serialization. serializationinfo info, system. runtime. serialization. streamingcontext context ): Base (Info, context)

{



}


}


There are several advantages to doing so.

1. The first three constructors provide continuous implementation methods

2. The last constructor is an essential constructor when the object is deserialized using soap or binary formattor. Otherwise, the exception thrown by your remoting Object Server cannot be bubble to the client.

I will write a simple soap formattor example to simulate the transfer of the remoting object.

// Todo 1 soap serialization

Xxxexception ex = New Xxxexception ( " Invalid. " );




Filestream FS = New Filestream ( " Soap. xml " , Filemode. Create );




System. runtime. serialization. formatters. Soap. soapformatter SR = New System. runtime. serialization. formatters. Soap. soapformatter ();



Sr. serialize (FS, ex );

FS. Flush ();

FS. Close ();


// Todo 2 soap serialization

Xxxexception exp;


Filestream fs1 = New Filestream ( " Soap. xml " , Filemode. Open );



System. runtime. serialization. formatters. Soap. soapformatter DSR = New System. runtime. serialization. formatters. Soap. soapformatter ();



Exp = (Xxxexception) DSR. deserialize (fs1 );



Fs1.close ();

MessageBox. Show (exp. Message );


If there is no fourth constructor of xxxexception, there may be

An unhandled exception of Type 'System. runtime. serialization. serializationexception' occurred in mscorlib. dll

Additional information: the constructor to deserialize an object of Type exceptiondemo. xxxexception was not found.

Of course, this is for remoting. The Web Service uses XML serilizer. Currently, this version does not support serialization of fields with declarative permission requirements in attributes. Therefore, in Web service, you cannot directly pass exception to the client.

For example

[Webmethod]

Public String helloworld (string S, out exception ex)

{

Ex = new formatexception ("fasdf ");

Return "Hello World ";

}

You will find that this example cannot be called on the client.

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.