【问题标题】:How to check exception of a member variable of an object?如何检查对象的成员变量的异常?
【发布时间】:2014-07-26 07:35:47
【问题描述】:

有没有办法检查对象的成员变量的异常?

例如,我有一个名为 rcp 的 Recipient 对象。 AddressEntry 是该对象的成员。我想在使用 AddressEntry 之前检查成员的异常情况。

我想编写一个方法来检查成员变量,但我没有想法。不要使用 try-catch

private voice GetEmail(Outlook.NameSpace otl, string email){ 
    //...
    Recipient rcp = otl.CreateRecipient(email);
    if (rcp != null && CheckException(rcp))
    {
      //do my code
    }
    //...
}

private bool CheckException(Recipient rcp)
{
    //if AddressEntry of rcp object does not threw exception, return true            

    return false;
}

您能给我一些建议或建议吗?

【问题讨论】:

    标签: c# winforms exception outlook


    【解决方案1】:

    基于异常进行控制流被称为反模式

    您需要以一种不知道是否会引发异常的方式开发代码,如果发生了,您需要捕获这些异常并恢复您的应用程序或通过以下方式向用户显示错误通知整个应用程序即将崩溃的用户界面。

    例外是例外情况,您需要关注常规情况

    我认为你的代码应该是这样的:

    try
    {
      Recipient rcp = otl.CreateRecipient(email);
    
      if (rcp != null)
      {
      //do my code
      }
    //...
    }
    catch(COMException e)
    {
         // Show a message box, alert, whatever relevant to your users
    }
    

    【讨论】:

      猜你喜欢
      • 2011-03-28
      • 1970-01-01
      • 2011-01-13
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多