代码规范 自定义 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";
  }

你会发现这个例子无法在客户端调用,呵呵。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值