Question Error Handling

d1973z28

New member
Joined
Jun 24, 2010
Messages
3
Programming Experience
Beginner
I've been working on a project that is almost done. It works great except for handling two types of possible errors in the section that emails an attachment. If there are no errors, it works perfectly. If one or the other error is present it still works. But if BOTH errors are present, it crashes. I have indicated "CRASHES HERE" in the first Catch Block.

How I'm testing:

Scenario #1: Works
A valid format email address is stored in the FromAddress variable
My network connection is enabled

Scenario #2: Works
A valid format email address is stored in the FromAddress variable
My network connection is disabled

Scenario #3: Works
A invalid format email address is stored in the FromAddress variable
(The program successfully changes the email address)
My network connection is enabled

Scenario #4: EPIC FAIL
A invalid format email address is stored in the FromAddress variable
My network connection is disabled


Any help would be great:

VB.NET:
 Try

            Dim Attachment As New System.Net.Mail.Attachment(MyFile)
            Dim Email As New System.Net.Mail.MailMessage(FromAddress, "ToAddress")

            Email.Subject = "Here's my File"
            Email.Body = "~File Attached~"

            Dim MailClient As New System.Net.Mail.SmtpClient()
            Dim Authentication As New System.Net.NetworkCredential("username", "password")

            MailClient.Host = "MyMailServerName"
            MailClient.UseDefaultCredentials = False
            MailClient.Credentials = Authentication

            Email.Attachments.Add(Attachment)
            MailClient.Send(Email)


            'Error handling: If FromAddress value is bad format replace with "me@some-domain.com"
            'and try to send again: m
        Catch AddressError As System.FormatException

            MsgBox("eMail Address ERROR found")

            Dim Attachment As New System.Net.Mail.Attachment(MyFile)
            Dim Email As New System.Net.Mail.MailMessage("me@some-domain.com", "ToAddress")
	     Email.Subject = "Here's my File"
            Email.Body = "~File Attached~"

            Dim MailClient As New System.Net.Mail.SmtpClient()
            Dim Authentication As New System.Net.NetworkCredential("username", "password")

            MailClient.Host = "MyMailServerName" 'ex: mail.some-domain.com
            MailClient.UseDefaultCredentials = False
            MailClient.Credentials = Authentication

            Email.Attachments.Add(Attachment)
            MailClient.Send(Email)  'CRASHES HERE

            'Error handling: Catch anything else (communications error)
        Catch 'CommunicationError As Exception 

	     MsgBox("ERROR Found, message did not send")

        End Try
 
The Try/Catch is only going to catch the AddressError error. If another error occurs in the AddressError catch routine, as it does in your scenario, the second error will go unhandled.

The AddressError exception is being thrown and then the connection error is occurring inside the AddressError catch block. You could put another try/catch in your AddressError block and catch the connection error. That should solve your problem.

If there are any other errors left unhandled, they would then cause your program to crash. A better solution might be to forget about trying to recover from the error, report it to the user and make them fix it.
 
VB.NET:
Dim adr As MailAddress
Try
   adr = New MailAddress(fromAddress)
Catch e As FormatException
   adr = New MailAddress(fallback)
End Try

Dim msg As New MailMessage
msg.From = adr
etc....
My network connection is disabled
Check with My.Computer.Network.IsAvailable property. Problems can still occur, so you must Try-Catch the Send call regardless.
 
Back
Top