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.