Pregunta

I've a console application that zips files and then send them via email. It does that every hour. I wanted to know what kind of exceptions should I handle ? Let say if there's no network available when the process starts. What exception will I get then ? And what can be the other ways this can fail. So basically I'm trying to figure out what exceptions I should catch.

I got something like this

try
{
    // zips files and send email
}      
catch(System.Net.Mail.SmtpException e)
{
    Console.WriteLine(e.toString());
}
catch(exception e)
{

}
¿Fue útil?

Solución

Ideally, the libraries you're using will come with documentation that will list all of the types of exceptions that can be thrown, if not then you'll have to use a tool like Reflector to inspect the methods you're using to find their thrown exceptions.

For example, the SmtpClient.Send method (as documented here http://msdn.microsoft.com/en-us/library/swas0fwc.aspx ) lists these exceptions:

  • ArgumentNullException
  • InvalidOperationException
  • ObjectDisposedException
  • SmtpException
  • SmtpFailedRecipientsException

Remember to catch exceptions in descending order of derivity, i.e. catch SmtpFailedRecipientsException before SmtpException because SmtpFailedRecipientsException derives from SmtpException.

Otros consejos

You can review the MSDN pages to view what types of exceptions certain methods or constructors will throw from .NET libraries. Such as, for the SmtpClient.Send method, it throws the following exceptions:

  • ArgumentNullException
  • InvalidOperationException
  • ObjectDisposedException
  • SmtpException
  • SmtpFailedRecipientsException

There is also a link of common exception types that you may be interested in located here.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top