How to properly use try-catch block?

Discussion in 'ASP .Net' started by GS, Jan 10, 2006.

  1. GS

    GS Guest

    Hi,

    Is it possible to use try-catch block to return back to processing on the same line where last error occured?
    Say I'm trying to establish connection to database in try {} and catching SQLException, if I find out that it's timeout issue then I want to try again to see if database is available.
    Is it possible to accomplish with try-catch block?

    G
     
    GS, Jan 10, 2006
    #1
    1. Advertising

  2. for (int i=0; i<3; i++)
    try
    {
    // connect
    }
    catch (Exception ex)
    {
    if (exception is not due to timeout)
    throw new Exception(..);
    }

    Eliyahu
    "GS" <> wrote in message news:...
    Hi,

    Is it possible to use try-catch block to return back to processing on the same line where last error occured?
    Say I'm trying to establish connection to database in try {} and catching SQLException, if I find out that it's timeout issue then I want to try again to see if database is available.
    Is it possible to accomplish with try-catch block?

    G
     
    Eliyahu Goldin, Jan 10, 2006
    #2
    1. Advertising

  3. if (exception is not do to timeout)
    {
    throw;
    }

    no need to throw a new one, just rethrow the actual exception

    Karl

    "Eliyahu Goldin" <> wrote in message news:...
    for (int i=0; i<3; i++)
    try
    {
    // connect
    }
    catch (Exception ex)
    {
    if (exception is not due to timeout)
    throw new Exception(..);
    }

    Eliyahu
    "GS" <> wrote in message news:...
    Hi,

    Is it possible to use try-catch block to return back to processing on the same line where last error occured?
    Say I'm trying to establish connection to database in try {} and catching SQLException, if I find out that it's timeout issue then I want to try again to see if database is available.
    Is it possible to accomplish with try-catch block?

    G
     
    Karl Seguin [MVP], Jan 10, 2006
    #3
  4. GS

    GS Guest

    Would not that mean that it'll try to establish connection 3 times even if connection was propery established first time?
    "Eliyahu Goldin" <> wrote in message news:...
    for (int i=0; i<3; i++)
    try
    {
    // connect
    }
    catch (Exception ex)
    {
    if (exception is not due to timeout)
    throw new Exception(..);
    }

    Eliyahu
    "GS" <> wrote in message news:...
    Hi,

    Is it possible to use try-catch block to return back to processing on the same line where last error occured?
    Say I'm trying to establish connection to database in try {} and catching SQLException, if I find out that it's timeout issue then I want to try again to see if database is available.
    Is it possible to accomplish with try-catch block?

    G
     
    GS, Jan 10, 2006
    #4
  5. Depends how you implement it

    for (int i = 0; i < 3; ++i)
    {
    try
    {
    connection.open();
    return connection;
    }Catch(SqlException ex)
    {
    if (Exception is not due to timeout)
    {
    throw;
    }
    }
    }

    the connection will be returned when it's successful and break out of the for loop.

    On a side note, If your database is timing out, I'm not sure if you really want to keep trying. As far as I'm concerned, you have some serious problems if timeouts are happening and simply continuing to try to connect is a bandaid solution.

    Karl
    "GS" <> wrote in message news:...
    Would not that mean that it'll try to establish connection 3 times even if connection was propery established first time?
    "Eliyahu Goldin" <> wrote in message news:...
    for (int i=0; i<3; i++)
    try
    {
    // connect
    }
    catch (Exception ex)
    {
    if (exception is not due to timeout)
    throw new Exception(..);
    }

    Eliyahu
    "GS" <> wrote in message news:...
    Hi,

    Is it possible to use try-catch block to return back to processing on the same line where last error occured?
    Say I'm trying to establish connection to database in try {} and catching SQLException, if I find out that it's timeout issue then I want to try again to see if database is available.
    Is it possible to accomplish with try-catch block?

    G
     
    Karl Seguin [MVP], Jan 10, 2006
    #5
    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. bienwell
    Replies:
    4
    Views:
    3,904
    bienwell
    May 27, 2005
  2. jtl.zheng
    Replies:
    14
    Views:
    859
    jtl.zheng
    Aug 11, 2006
  3. JJ297

    Catch... try block

    JJ297, Oct 31, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    417
    JJ297
    Oct 31, 2007
  4. Julia B
    Replies:
    2
    Views:
    285
    Julia B
    Nov 21, 2007
  5. Savvoulidis Iordanis
    Replies:
    7
    Views:
    585
    Göran Andersson
    Jul 20, 2008
Loading...

Share This Page