How to properly use try-catch block?

G

GS

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
 
E

Eliyahu Goldin

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
 
K

Karl Seguin [MVP]

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
 
G

GS

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
 
K

Karl Seguin [MVP]

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top