473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to properly use try-catch block?

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
Jan 10 '06 #1
4 1351
for (int i=0; i<3; i++)
try
{
// connect
}
catch (Exception ex)
{
if (exception is not due to timeout)
throw new Exception(..);
}

Eliyahu
"GS" <no@no.com> wrote in message news:u3**************@TK2MSFTNGP10.phx.gbl...
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
Jan 10 '06 #2
if (exception is not do to timeout)
{
throw;
}

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

Karl

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

Eliyahu
"GS" <no@no.com> wrote in message news:u3**************@TK2MSFTNGP10.phx.gbl...
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
Jan 10 '06 #3
GS
Would not that mean that it'll try to establish connection 3 times even if connection was propery established first time?
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message news:eB**************@TK2MSFTNGP15.phx.gbl...
for (int i=0; i<3; i++)
try
{
// connect
}
catch (Exception ex)
{
if (exception is not due to timeout)
throw new Exception(..);
}

Eliyahu
"GS" <no@no.com> wrote in message news:u3**************@TK2MSFTNGP10.phx.gbl...
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
Jan 10 '06 #4
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" <no****@nowhere.com> wrote in message news:eh**************@tk2msftngp13.phx.gbl...
Would not that mean that it'll try to establish connection 3 times even if connection was propery established first time?
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message news:eB**************@TK2MSFTNGP15.phx.gbl...
for (int i=0; i<3; i++)
try
{
// connect
}
catch (Exception ex)
{
if (exception is not due to timeout)
throw new Exception(..);
}

Eliyahu
"GS" <no@no.com> wrote in message news:u3**************@TK2MSFTNGP10.phx.gbl...
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
Jan 10 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sumon | last post by:
Hi, I try to install "pear" on win2k with php 4.3.3 . in Ms/dos when I give C:\>pear list INSTALLED PACKAGES: =================== PACKAGE VERSION STATE Archive_Tar 1.1 stable...
8
by: weasel | last post by:
Why is the Farenheit to Celsius part not working properly? Instead of showing a similar range of what the farenheit is listing, the celsius portion is showing half the range of farenheit. print...
3
by: N. Okan Guney | last post by:
Hi, When attempting to debug a project on a remote server, I get the error message : "Error while trying to run project: unable to start debugging on the web server. the debugger is not...
8
by: Jaime Rios | last post by:
Hi, I created a COM AddIn for Word that performs the functions that it needs to, but I needed to add the ability for the toolbar created by the COM AddIn to remember it's last position and...
0
by: benny | last post by:
Hi, Anyone use cassini to deploy the asp.net web application face the following problem? I have the asp.net web application that consist the login page and Default page where I always face the...
4
by: Tommy Vercetti | last post by:
I've been trying to convert a Standard C++/MFC application to Managed C++/Forms (the application is pretty complex but the GUI is just a simple status dialog). I created a new project shell and...
1
by: John Todd | last post by:
Hi, all. I've been working on a .NET project written in C# for the past three months. Yesterday, all of a sudden, I get The application failed to initialize properly (0xc0000005) error when i...
21
by: onkar | last post by:
#include<stdio.h> int i; int i; int main(){ printf("i=%d\n",i); return 0; } Note : I am using gcc-3.4.3 on i386-redhat-linux
15
by: ianweise | last post by:
hello, before i post my code for this, is there anyone out there at this moment? no sense in posting if no one is out there to read and answer it =P
7
by: robin1983 | last post by:
Hi, good morning everyone, i have a file called attendence.php The problem is that some part of code is executing properly and half of the code is not and i dont get any warning or error message. For...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.