代码规范 自定义 Exception

代码规范 自定义 Exception

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

None.gif
    [Serializable ]
None.gif    

public
 
class
 XXXException:ApplicationException
ExpandedBlockStart.gifContractedBlock.gif    

dot.gif
{
InBlock.gif        
public XXXException():base()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public XXXException(string message):base(message)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public XXXException(string message,Exception innerException):base(message,innerException)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected XXXException ( System.Runtime.Serialization.SerializationInfo info , System.Runtime.Serialization.StreamingContext context ):base(info,context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif    }

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

我写一个简单的soap formattor 的例子来模拟remoting 对象的传递

None.gif
    
//
TODO 1 soap 序列化

None.gif

            XXXException ex
=
new
 XXXException(

invaliddot.gif.

);
None.gif            
None.gif
None.gif            FileStream fs

=
new
 FileStream(

soap.xml

,FileMode.Create );
None.gif
None.gif            
None.gif            System.Runtime.Serialization.Formatters.Soap.SoapFormatter sr

=
new
 System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
None.gif             
None.gif            sr.Serialize(fs,ex);
None.gif            fs.Flush();
None.gif            fs.Close();
None.gif
None.gif            

//
TODO 2 soap 翻序列化

None.gif

            XXXException exp;
None.gif
None.gif            FileStream fs1

=
new
 FileStream(

soap.xml

,FileMode.Open);
None.gif            
None.gif            System.Runtime.Serialization.Formatters.Soap.SoapFormatter dsr

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

=
(XXXException)dsr.Deserialize(fs1);
None.gif            
None.gif            fs1.Close();
None.gif            MessageBox.Show(exp.Message);

如果没有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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号