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. Advertisements

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

    Eliyahu
    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. Advertisements

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

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

    Karl

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

    Eliyahu
    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?
    for (int i=0; i<3; i++)
    try
    {
    // connect
    }
    catch (Exception ex)
    {
    if (exception is not due to timeout)
    throw new Exception(..);
    }

    Eliyahu
    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
    Would not that mean that it'll try to establish connection 3 times even if connection was propery established first time?
    for (int i=0; i<3; i++)
    try
    {
    // connect
    }
    catch (Exception ex)
    {
    if (exception is not due to timeout)
    throw new Exception(..);
    }

    Eliyahu
    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. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.