locked
Retry Catch RRS feed

  • Question

  • Code Snippet

    redo:
    try
    {
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.google.com");
    req.Timeout = 10000;
    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
    }
    catch(TimeoutException e)
    {
    Console.WriteLine(e.Message);
    goto redo;
    }


    Is there a better way to retry a site if timed out, without using 'goto'?
    Monday, February 4, 2008 8:15 PM

Answers

  • //Probably want some counter to stop the loop after certain number of attempts

     

    while(true)

    {

        try

        {

            ... your code here.

        }

        catch(TimeOutException e)

        {

     

        }

     

        //case to stop looping

        if(......)

        {

            //Leave the loop

            break;       

        }

    }

     

    Monday, February 4, 2008 8:42 PM
  • Labels and goto's? Smile

    You can create a function like this:

    boolean MyClass.HttpConnect() {
    boolean result = true;
    try
    {
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.google.com");
    req.Timeout = 10000;
    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
    }
    catch(TimeoutException e)
    {
    Console.WriteLine(e.Message);
    result = false;
    }

    return result;
    }



    You can retry 10(for example) to conenct to that site:

    int i = 0;
    while(!HttpConnect() && i++ < 10);


    Regards
    Monday, February 4, 2008 8:51 PM
  • My pattern for retry loops looks like this:

    Code Snippet
        bool redo = false;
        const int maxRetries = 3;
        int retries = 0;
        do
        {
            try
            {
                HttpWebRequest req = (HttpWebRequest) WebRequest.Create("http://www.google.com");
                req.Timeout = 10000;
                HttpWebResponse res = (HttpWebResponse) req.GetResponse();
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e.Message);
                redo = true;
                ++retries;
            }
            while (redo && retries < maxRetries) ;
        } 

     

     

     

    Monday, February 4, 2008 9:14 PM
    Moderator

All replies

  • //Probably want some counter to stop the loop after certain number of attempts

     

    while(true)

    {

        try

        {

            ... your code here.

        }

        catch(TimeOutException e)

        {

     

        }

     

        //case to stop looping

        if(......)

        {

            //Leave the loop

            break;       

        }

    }

     

    Monday, February 4, 2008 8:42 PM
  • Labels and goto's? Smile

    You can create a function like this:

    boolean MyClass.HttpConnect() {
    boolean result = true;
    try
    {
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.google.com");
    req.Timeout = 10000;
    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
    }
    catch(TimeoutException e)
    {
    Console.WriteLine(e.Message);
    result = false;
    }

    return result;
    }



    You can retry 10(for example) to conenct to that site:

    int i = 0;
    while(!HttpConnect() && i++ < 10);


    Regards
    Monday, February 4, 2008 8:51 PM
  • My pattern for retry loops looks like this:

    Code Snippet
        bool redo = false;
        const int maxRetries = 3;
        int retries = 0;
        do
        {
            try
            {
                HttpWebRequest req = (HttpWebRequest) WebRequest.Create("http://www.google.com");
                req.Timeout = 10000;
                HttpWebResponse res = (HttpWebResponse) req.GetResponse();
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e.Message);
                redo = true;
                ++retries;
            }
            while (redo && retries < maxRetries) ;
        } 

     

     

     

    Monday, February 4, 2008 9:14 PM
    Moderator
  • Thanks everyone! Smile
    Monday, February 4, 2008 9:37 PM