再谈异常处理try catch finally


 1. 查找系统中坏味道的异常处理代码

在上篇文章杂谈异常处理try-catch-finally中主要详细介绍了C#异常处理的概念,异常设计准则,基础知识等方面的内容,但对如何正确使用异常处理印象还不是特别深刻吧。在这篇中,我通过查找以前系统代码中存在坏味道的异常处理代码来分析和讲解如何正确使用异常处理。

1.1. 例一

/// <summary>
/// 保存记录
/// </summary>
/// <param name="entity">实体</param>
public virtual object Save(T entity)
{
ISession session = NHibernateUtils.GetCurrentSession();
ITransaction tx = null;
try
{
tx = session.BeginTransaction();
object id = session.Save(entity);
tx.Commit();
return id;
}
catch (HibernateException ex)
{
if (tx != null) tx.Rollback();
throw ex;
}
finally
{
NHibernateUtils.CloseSession();
}
}

上面代码中其中

catch (HibernateException ex)
{
if (tx != null) tx.Rollback();
throw ex;
}

代码在异常设计是不是与“如果了解特定异常在给定上下文中引发的条件,请考虑捕捉这些异常。”或“捕捉并再次引发异常时,首选使用空引发。这是保留异常调用堆栈的最佳方式。”的要求不太相符合呢?把throw ex;改成throw;或throw new RepositoryLayerException("保存记录发生错误!", ex);是不是符合上面两条异常设计准则了呢?

1.2. 例二

表示层代码

try
{
Cursor = Cursors.WaitCursor;
IApplicationContext ctx = ContextRegistry.GetContext();
IStoreRepository storeRepository = ctx.GetObject("StoreRepository") as IStoreRepository;
StoreInfo store = Store;
store.Name = XXName.Text.Trim();
storeRepository.Save(store);
}
catch (Exception ex)
{
ShowMessageBox(ex, MessageLevel.Error);
}
finally
{
Cursor = Cursors.Arrow;
}

领域层代码

private string _name;
/// <summary>
/// 店铺名称
/// </summary>
public virtual string Name
{
set
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException("value", "店铺名称不能为空!");
}
if (ValidationUtils.GetLength(value,CharacterType.NVarChar) > 200)
{
throw new ArgumentOutOfRangeException("value", "店铺名称不能大于200!");
}
_name = value;
}
get { return _name; }
}

在这里需要说明一下,其中XXName是一个文本框控件,如果XXName文本框输入的值经常超过200个字符,会不停的弹出错误消息框,这样是不是会引起效率问题,涉及到效率问题我们会想到什么,对了就是Tester-Doer 模式,呵呵,在这里我们怎么应用这个模式呢?其实很简单,我们只要设置this.XXName.MaxLength = 200;是不是就解决了这个问题?

1.3. 思考总结

认真去分析,会发现在系统中坏味道的代码还真不少,原来在异常处理中自认为比较正确的做法,比较优美的做法发现都是存在问题的。经过这几天对异常处理的系统学习研究,对系统中不好做法的异常处理进行排查,还是学到了不少知识,也发现自己的不足,原来很多基础技术知识还可以更上一层楼。

相关阅读:杂谈try-catch-finally异常处理,非常感谢大家一直以来对phpstudy网站的支持。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3