Menu

Add ThrowExceptions property

Ron
2005-09-29
2013-04-02
  • Ron

    Ron - 2005-09-29

    Sometimes its cumbersome to deal with errors:

    // warning: FromAddress missing!
    EmailMessage emailMessage = null;
    try
    {
      emailMessage = new EmailMessage();
      if (emailMessage.Send(smtp) == false)
      {
       log.Error("Unable to send mail.");
      }
    }
    catch (MailException ex)
    {
      StringBuilder sb = new StringBuilder("Unable to send mail.");
      if (emailMessage != null)
      {
       sb.Append(Environment.NewLine);
       sb.Append(emailMessage.ToDataString());
      }
      log.Error(sb.ToString(), ex);
    }
    catch (Exception ex)
    {
      if (emailMessage != null)
      {
       log.Error("Unable to send mail: " + emailMessage.ToDataString(), ex);
      }
      else
      {
       log.Error("Unable to send mail.", ex);
      }
    }

    If the EmailMessage object had a property indicating whether or not it should swallow its own exceptions it would be easier to extract this information later:

    // warning: FromAddress missing!
    EmailMessage emailMessage = new EmailMessage();
    emailMessage.ThrowExceptions = false;
    if (emailMessage.Send(smtp) == false)
    {
    log.Error(
      "Unable to send mail."+emailMessage.ToDataString(),
      emailMessage.GetLastException());
    }

    You would always have access to a non-null EmailMessage object that you could investigate and log if the email was not send successfully.

     
    • Mike Bridge

      Mike Bridge - 2005-09-29

      One of my beefs about C# is that it the compiler doesn't force you to acknowledge potential exceptions, making it dangerous to use anything outside of a try-catch block.  I feel it's a violation of the calling contract, and it means that unless you really trust the documentation and love to read it, you need to do one catch-all block somewhere with a "catch (Exception ex)". 

      That said, my philosophy is that an exception should be thrown for abnormal behaviour (e.g. there is a network error, a problem with the SMTP server, etc.), and I generally leave it up to the caller to catch it.  It certainly makes for nicer error-handling code in the module.  But that's my personal preference....

       
    • Ron

      Ron - 2005-09-29

      This was the case that made me suggest the idea:

      EmailMessage emailMessage = null;
      try
      {
      emailMessage = new EmailMessage();
      emailMessage.AddToAddress(new DotNetOpenMail.EmailAddress(toEmail));
      // emailMessage.FromAddress = new DotNetOpenMail.EmailAddress(fromEmail);
      emailMessage.Subject = subject;
      emailMessage.TextPart = new TextAttachment(textBody);
      emailMessage.Send(smtpServer);
      }
      catch (MailException ex)
      {
      if (emailMessage != null)
      {
        emailLog.Info(emailMessage.ToDataString(), ex);
      }
      }
      catch (Exception ex)
      {
      emailLog.Error("Unable to send mail.", ex);
      }

      [NullReferenceException: Object reference not set to an instance of an object.] 

      DotNetOpenMail.EmailMessage.GetStandardHeaders(Encoding charset, IEncoder encoder) +46  

      DotNetOpenMail.EmailMessage.ToDataStringHeaders(Encoding charset, IEncoder encoder) +44

      DotNetOpenMail.EmailMessage.ToDataString() +27

      If emailMessage is not null how do I know if its safe to call ToDataString()?

       
      • Mike Bridge

        Mike Bridge - 2005-09-30

        If it's throwing a null pointer exception, that'd be a bug---I'll look at that.  Hence my distaste for the looseness of the C# Exception handling. :)

        -Mike

        P.S. Technically, my ToDataString() method should have an "internal" declaration, but because my NUnit testing code is in a separate Project, I made it public.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.