Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How to properly use try-catch block?

Reply
Thread Tools

How to properly use try-catch block?

 
 
GS
Guest
Posts: n/a
 
      01-10-2006
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
 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      01-10-2006
for (int i=0; i<3; i++)
try
{
// connect
}
catch (Exception ex)
{
if (exception is not due to timeout)
throw new Exception(..);
}

Eliyahu
"GS" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
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
 
Reply With Quote
 
 
 
 
Karl Seguin [MVP]
Guest
Posts: n/a
 
      01-10-2006
if (exception is not do to timeout)
{
throw;
}

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

Karl

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

Eliyahu
"GS" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
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
 
Reply With Quote
 
GS
Guest
Posts: n/a
 
      01-10-2006
Would not that mean that it'll try to establish connection 3 times even if connection was propery established first time?
"Eliyahu Goldin" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
for (int i=0; i<3; i++)
try
{
// connect
}
catch (Exception ex)
{
if (exception is not due to timeout)
throw new Exception(..);
}

Eliyahu
"GS" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
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
 
Reply With Quote
 
Karl Seguin [MVP]
Guest
Posts: n/a
 
      01-10-2006
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" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
Would not that mean that it'll try to establish connection 3 times even if connection was propery established first time?
"Eliyahu Goldin" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
for (int i=0; i<3; i++)
try
{
// connect
}
catch (Exception ex)
{
if (exception is not due to timeout)
throw new Exception(..);
}

Eliyahu
"GS" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use Internal Linkage Properly? Immortal_Nephi@hotmail.com C Programming 4 09-22-2008 06:37 AM
Canon A 610 - how to properly use macro sweetangel Digital Photography 4 01-20-2006 02:46 PM
What Objects To Use & How To Properly Bind Jim in Arizona ASP .Net 2 07-06-2005 07:25 PM
xsl:sort - how do i use it properly? Angus Parvis XML 0 11-16-2004 07:40 PM
More American Graffiti: Properly Framed, Properly Scored? Scot Gardner DVD Video 0 09-02-2003 02:28 AM



Advertisments