How to catch exceptions?

Discussion in 'ASP .Net' started by JackPot, Jul 24, 2008.

  1. JackPot

    JackPot Guest

    Actually, I'm interested in learning where the different types of exceptions
    are documented for a specific type of class, let us say for instance classes
    in the System.Net.Mail Namespace. Shouldn't the types be documented on the
    same page as the class, its members and properties?
    JackPot, Jul 24, 2008
    #1
    1. Advertising

  2. JackPot

    Munna Guest

    Hi


    http://msdn.microsoft.com/en-us/library/system.net.mail.aspx

    SmtpException: Represents the exception that is thrown when the
    SmtpClient is not able to complete a Send or SendAsync operation.
    SmtpFailedRecipientException: Represents the exception that is thrown
    when the SmtpClient is not able to complete a Send or SendAsync
    operation to a particular recipient.
    SmtpFailedRecipientsException: Infrastructure. The exception that is
    thrown when e-mail is sent using an SmtpClient and cannot be delivered
    to all recipients.

    yes they are mentioned there...

    Best of luck

    -------
    Munna

    www.munna.shatkotha.com/blog
    www.munna.shatkotha.com
    www.shatkotha.com
    Munna, Jul 24, 2008
    #2
    1. Advertising

  3. JackPot

    JackPot Guest

    Oh yea I see, exceptions are classes and listed in the section for classes
    which will be typical for any namespace. Duh.

    "Munna" <> wrote in message
    news:...
    > Hi
    >
    >
    > http://msdn.microsoft.com/en-us/library/system.net.mail.aspx
    >
    > SmtpException: Represents the exception that is thrown when the
    > SmtpClient is not able to complete a Send or SendAsync operation.
    > SmtpFailedRecipientException: Represents the exception that is thrown
    > when the SmtpClient is not able to complete a Send or SendAsync
    > operation to a particular recipient.
    > SmtpFailedRecipientsException: Infrastructure. The exception that is
    > thrown when e-mail is sent using an SmtpClient and cannot be delivered
    > to all recipients.
    >
    > yes they are mentioned there...
    >
    > Best of luck
    >
    > -------
    > Munna
    >
    > www.munna.shatkotha.com/blog
    > www.munna.shatkotha.com
    > www.shatkotha.com
    JackPot, Jul 24, 2008
    #3
  4. JackPot

    Moon Guest

    CallBack for Asynch does not work consistently. i.e. quite often it does not
    get triggered. Can Anyone help?

    "JackPot" wrote:

    > Oh yea I see, exceptions are classes and listed in the section for classes
    > which will be typical for any namespace. Duh.
    >
    > "Munna" <> wrote in message
    > news:...
    > > Hi
    > >
    > >
    > > http://msdn.microsoft.com/en-us/library/system.net.mail.aspx
    > >
    > > SmtpException: Represents the exception that is thrown when the
    > > SmtpClient is not able to complete a Send or SendAsync operation.
    > > SmtpFailedRecipientException: Represents the exception that is thrown
    > > when the SmtpClient is not able to complete a Send or SendAsync
    > > operation to a particular recipient.
    > > SmtpFailedRecipientsException: Infrastructure. The exception that is
    > > thrown when e-mail is sent using an SmtpClient and cannot be delivered
    > > to all recipients.
    > >
    > > yes they are mentioned there...
    > >
    > > Best of luck
    > >
    > > -------
    > > Munna
    > >
    > > www.munna.shatkotha.com/blog
    > > www.munna.shatkotha.com
    > > www.shatkotha.com

    >
    >
    Moon, Aug 5, 2008
    #4
  5. Moon wrote:
    > CallBack for Asynch does not work consistently. i.e. quite often it does not
    > get triggered. Can Anyone help?


    Don't ask a totally unrelated question in an existing thread. Start a
    new thread, and explain what it is that you are doing. There are several
    classes with asynchronous methods, specify which one is it that you are
    using.

    --
    Göran Andersson
    _____
    http://www.guffa.com
    Göran Andersson, Aug 5, 2008
    #5
  6. JackPot

    Moon Guest

    Dear Goran,

    I believe this thread is about SMTPClient exceptions. That is what my
    question is about. I am trying to catch the exception from an async
    SMTPCleint send via the callback sendcompleted functionality.

    I felt that this was appropriate for this thread.

    I apologise if you still think this is not the case.

    Moon


    "Göran Andersson" wrote:

    > Moon wrote:
    > > CallBack for Asynch does not work consistently. i.e. quite often it does not
    > > get triggered. Can Anyone help?

    >
    > Don't ask a totally unrelated question in an existing thread. Start a
    > new thread, and explain what it is that you are doing. There are several
    > classes with asynchronous methods, specify which one is it that you are
    > using.
    >
    > --
    > Göran Andersson
    > _____
    > http://www.guffa.com
    >
    Moon, Aug 5, 2008
    #6
  7. Moon wrote:
    > Dear Goran,
    >
    > I believe this thread is about SMTPClient exceptions. That is what my
    > question is about. I am trying to catch the exception from an async
    > SMTPCleint send via the callback sendcompleted functionality.
    >
    > I felt that this was appropriate for this thread.
    >
    > I apologise if you still think this is not the case.
    >
    > Moon


    I see. It seemed totally irrelevant as your question is about
    asynchronous calls. An asynchronous process doesn't throw exceptions to
    communicate back to the caller.

    The documentation says that you need to wait for the previos send to
    complete before sending again, does your code follow that restriction?

    What you are talking about doesn't seem to be a common problem, so it's
    probably related to your specific code. If you posted some of it, I (or
    anyone else here) can take a look at it.

    >
    > "Göran Andersson" wrote:
    >
    >> Moon wrote:
    >>> CallBack for Asynch does not work consistently. i.e. quite often it does not
    >>> get triggered. Can Anyone help?

    >> Don't ask a totally unrelated question in an existing thread. Start a
    >> new thread, and explain what it is that you are doing. There are several
    >> classes with asynchronous methods, specify which one is it that you are
    >> using.
    >>
    >> --
    >> Göran Andersson
    >> _____
    >> http://www.guffa.com
    >>



    --
    Göran Andersson
    _____
    http://www.guffa.com
    Göran Andersson, Aug 5, 2008
    #7
  8. JackPot

    Moon Guest

    Hi Goran,

    Actually I had not spotted the need to wait before sending next - seems a
    bit odd for async. Howvever, I ammended my code and it works much better. But
    I do get erros which are now caught by CallBack, BUT for no obvious reason.
    If I immediately resend the same message it then works...

    If have included code below: if you look at my log at the end, you can see I
    try to send and email "TEST EMAIL NC 11:29:10" then I am blocked from sending
    by my own code whilst I wait fr complete callback - which too a few minutes.
    I get system error 4. Then I resend message and it works OK - see OnCompleted
    code to ensure I am sending exact same message with only difference being
    adding "#1" to front of subject.

    Odd eh?

    Lawrence


    private static void SendNetEmail(string To, string From, string
    Subject, string Body)
    {
    if (gWaitingForSendToComplete)
    {
    AppendDebug("Waiting for complete New Subject: " + Subject);
    return;
    }
    System.Net.Mail.MailMessage Mailer = new
    System.Net.Mail.MailMessage();
    Mailer.From = new System.Net.Mail.MailAddress( From);
    Mailer.To.Add(To);
    Mailer.Subject = Subject;
    Mailer.Body = Body;
    Mailer.IsBodyHtml = false;
    Mailer.Priority = System.Net.Mail.MailPriority.High;


    SmtpClient SC = new SmtpClient("hidden");
    SC.Credentials = new NetworkCredential("hidden", "hidden");
    SC.SendCompleted += new
    SendCompletedEventHandler(SmtpClient_OnCompleted);
    object UserState = Mailer;

    try
    {
    AppendDebug("Trying to send: " + Subject);
    SC.SendAsync(Mailer, UserState);
    gWaitingForSendToComplete = true;
    }
    catch (Exception ex)
    {
    gWaitingForSendToComplete = false;
    Exception ex2 = ex;
    string errorMessage = string.Empty;
    while (ex2 != null)
    {
    errorMessage += ex2.ToString();
    ex2 = ex2.InnerException;
    }

    AppendDebug(errorMessage);

    }

    }

    public static void SmtpClient_OnCompleted(object sender,
    AsyncCompletedEventArgs e)
    {
    gWaitingForSendToComplete = false;
    System.Net.Mail.MailMessage mail =
    (System.Net.Mail.MailMessage)e.UserState;
    AppendDebug("Callback: " + mail.Subject);

    if (e.Error == null)
    {
    AppendDebug("No errors");
    MessageBox.Show("SNM SENT No CallBack Errors" + mail.Subject
    );
    }
    else
    {
    AppendDebug("Errors " + mail.Subject + " " +
    e.Error.ToString());
    MessageBox.Show("SNM CallBack ERRORS" + mail.Subject + " " +
    e.Error.ToString());
    if (mail.Subject.StartsWith("#"))
    {
    int Num = int.Parse(mail.Subject.Substring(1, 1));
    if (Num >= 9)
    {
    AppendDebug("Gave up trying to send");
    return;
    }
    Num++;
    mail.Subject = "#" + Num + mail.Subject.Substring(2,
    mail.Subject.Length - 2);
    }
    else
    {
    mail.Subject = "#1" + mail.Subject;

    }
    Thread.Sleep(5000);
    SendNetEmail(mail.To.ToString(), mail.From.ToString(),
    mail.Subject, mail.Body);
    }
    }

    public static void AppendDebug(string Txt)
    {

    long ts = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
    TextWriter TW = new StreamWriter("C:\\DebugLog.txt", true);
    TW.WriteLine(ts.ToString() + " " + Txt);
    TW.Close();


    }



    My LOG from AppendDebug

    63353618950380 Trying to send: TEST EMAIL NC 11:29:10
    63353618957615 Sending mail
    63353618957615 Waiting for complete New Subject: TEST EMAIL NC 11:29:17
    63353618960771 Sending mail
    63353618960771 Waiting for complete New Subject: TEST EMAIL NC 11:29:20
    63353619032193 Callback: TEST EMAIL NC 11:29:10
    63353619032193 Errors TEST EMAIL NC 11:29:10 System.Net.Mail.SmtpException:
    Syntax error, command unrecognized. The server response was: System error 4
    at System.Net.Mail.SendMailAsyncResult.End(IAsyncResult result)
    at System.Net.Mail.SmtpTransport.EndSendMail(IAsyncResult result)
    at System.Net.Mail.SmtpClient.SendMailCallback(IAsyncResult result)
    63353619222350 Trying to send: #1TEST EMAIL NC 11:29:10
    63353619223303 Callback: #1TEST EMAIL NC 11:29:10
    63353619223303 No errors






    "Göran Andersson" wrote:

    > Moon wrote:
    > > Dear Goran,
    > >
    > > I believe this thread is about SMTPClient exceptions. That is what my
    > > question is about. I am trying to catch the exception from an async
    > > SMTPCleint send via the callback sendcompleted functionality.
    > >
    > > I felt that this was appropriate for this thread.
    > >
    > > I apologise if you still think this is not the case.
    > >
    > > Moon

    >
    > I see. It seemed totally irrelevant as your question is about
    > asynchronous calls. An asynchronous process doesn't throw exceptions to
    > communicate back to the caller.
    >
    > The documentation says that you need to wait for the previos send to
    > complete before sending again, does your code follow that restriction?
    >
    > What you are talking about doesn't seem to be a common problem, so it's
    > probably related to your specific code. If you posted some of it, I (or
    > anyone else here) can take a look at it.
    >
    > >
    > > "Göran Andersson" wrote:
    > >
    > >> Moon wrote:
    > >>> CallBack for Asynch does not work consistently. i.e. quite often it does not
    > >>> get triggered. Can Anyone help?
    > >> Don't ask a totally unrelated question in an existing thread. Start a
    > >> new thread, and explain what it is that you are doing. There are several
    > >> classes with asynchronous methods, specify which one is it that you are
    > >> using.
    > >>
    > >> --
    > >> Göran Andersson
    > >> _____
    > >> http://www.guffa.com
    > >>

    >
    >
    > --
    > Göran Andersson
    > _____
    > http://www.guffa.com
    >
    Moon, Aug 6, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver

    How do I catch unhandled exceptions

    Alan Silver, Mar 20, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    1,706
    Alan Silver
    Mar 23, 2006
  2. Chris Berg

    How to catch multiple Exceptions

    Chris Berg, Jan 19, 2004, in forum: Java
    Replies:
    8
    Views:
    15,684
    Chris Berg
    Jan 19, 2004
  3. John Black
    Replies:
    8
    Views:
    4,130
    Xenos
    Aug 20, 2004
  4. Adam
    Replies:
    9
    Views:
    542
    red floyd
    Feb 2, 2006
  5. Marteno Rodia

    catch doesn't catch a thrown exception

    Marteno Rodia, Aug 3, 2009, in forum: Java
    Replies:
    5
    Views:
    540
    Daniel Pitts
    Aug 5, 2009
Loading...

Share This Page