很多规范中都提到,自定义Exception 要求使用一种统一的构造函数,比如默认实现以下四个构造函数。

[Serializable ]

public
class
XXXException:ApplicationException



{





























这样做有几个好处
1. 前三个构造函数可以提供一直的实现方式
2. 最后一个构造函数是对象使用 soap 或者binary formattor 反序列化的时候比不可少的构造函数。否则你的remoting 对象服务端抛出的异常,无法bubble 到客户端。
我写一个简单的soap formattor 的例子来模拟remoting 对象的传递

//
TODO 1 soap 序列化

XXXException ex
=
new
XXXException(
“
invalid

“
);



=
new
FileStream(
“
soap.xml
“
,FileMode.Create );



=
new
System.Runtime.Serialization.Formatters.Soap.SoapFormatter();






//
TODO 2 soap 翻序列化

XXXException exp;


=
new
FileStream(
“
soap.xml
“
,FileMode.Open);


=
new
System.Runtime.Serialization.Formatters.Soap.SoapFormatter();


=
(XXXException)dsr.Deserialize(fs1);



如果没有xxxexception 的第四个构造函数,可能会有
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.
当然这个是针对 remoting,web service 走得是 xml serilizer,目前这个版本对 属性中有申明权限要求的字段无法序列化。所以web service 中,你是无法直接讲 exception 传递给客户端。
比如一下例子
[WebMethod]
public string HelloWorld(string s,out Exception ex)
{
ex=new FormatException(“fasdf”);
return “Hello World”;
}
你会发现这个例子无法在客户端调用,呵呵。
转载于:https://www.cnblogs.com/montaque/archive/2004/12/06/73358.html
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/111171.html原文链接:https://javaforall.net