0

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)
{

}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user2901683
  • 175
  • 1
  • 1
  • 12

2 Answers2

0

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.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • Note, that caller of `SmtpClient.Send` should never catch `ArgumentNullException`, `InvalidOperationException` and `ObjectDisposedException`. When these exceptions are raised, this means errors in caller code. – Dennis Nov 04 '13 at 05:42
  • Also, never "swallow" exceptions - log them if nothing else. – Dai Nov 04 '13 at 07:09
0

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.

matth
  • 6,112
  • 4
  • 37
  • 43