NHibernate使用时,不能返回自己的异常的解决办法

本文探讨了在使用NH框架时,自定义方法抛出异常的处理方式。通过具体示例展示了如何捕捉并处理这些异常,避免异常被NH的反射机制封装为TargetInvocationException。

    在使用NH时,如果定义一个类的方法,须定义成virtual类型,这样才可以让NH自动产生代理类来调用此方法.但这时,有一个问题:如果我们这个方法中抛出了自己定义的异常,在外面会能得到自己的异常吗?

None.gif    public class People
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
public virtual void Eat(string food)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (foot == "")
InBlock.gif                
throw new ArgumentException();
InBlock.gif
InBlock.gif            
if (foot == "")
InBlock.gif                
throw new BusinessException();
InBlock.gif
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void ShowMessage(string message)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{}
InBlock.gif
InBlock.gif
InBlock.gif        
public static void TestEatException()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Eat("");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (ArgumentException)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ShowMessage(
"没有东西吃");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (BusinessException)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ShowMessage(
"水应该不能吃,酒还说得过去");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception exp)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ShowMessage(
"不应该出现这种异常的" + exp.GetType().Name);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif

     想想TestEatException会出现什么异常?刚开始时,我以为是"水应该不能吃,酒还说得过去",但我错了.
     正确结果是"不应该出现这种异常TargetInvocationException".想来也是NH的反射搞得鬼.
但如果我们调试一下,发现异常的InnerException是我们自定义的期望抛出来的异常.哈哈.
     临时解决方法如下:
None.gif    public class People
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
public virtual void Eat(string food)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (foot == "")
InBlock.gif                
throw new ArgumentException();
InBlock.gif
InBlock.gif            
if (foot == "")
InBlock.gif                
throw new BusinessException();
InBlock.gif
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void ShowMessage(string message)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{}
InBlock.gif
InBlock.gif
InBlock.gif        
public static void TestEatException()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.Eat("");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch (TargetInvocationException exp)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (exp.InnerException != null)
InBlock.gif                        
throw exp.InnerException;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (ArgumentException)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ShowMessage(
"没有东西吃");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (BusinessException)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ShowMessage(
"水应该不能吃,酒还说得过去");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception exp)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ShowMessage(
"不应该出现这种异常的" + exp.GetType().Name);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif

     这时就会出现"水应该不能吃,酒还说得过去"的信息了.如果您有更好的办法,一定要分享呀!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值