473,659 Members | 3,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

try/catch

Normally, I surround my Dataset/fill or DBreader execut with a try/Catch.

Something like:
*************** *************** *************** *********
Dim dbReader As SqlDataReader

Dim ConnectionStrin g as String
=System.Configu ration.Configur ationSettings.A ppSettings("MM_ CONNECTION_STRI NG_Connection")
Dim objConn as New SqlConnection (ConnectionStri ng)
Dim CommandText as String = "Select City,StateCode from zipCodes where
zipCode = @ZipCode and CityType = 'D'"
Dim objCmd as New SqlCommand(Comm andText,objConn )
with objCmd.Paramete rs
.Add("@ZipCode" ,SqlDbType.VarC har,9).value = ZipCode.Text
end with

try
objConn.Open()

dbReader = objCmd.ExecuteR eader

if dbReader.Read then
City.Text = dbReader("City" )
State.Text = dbReader("State Code")
end if
catch ex as Exception
... Do something
finally
objConn.Close()
end try
*************** *************** *************** *************** **

I have someone here that writes his code where he surround all his code with
a try/catch, not just the area where he could logically expect to have a
problem. Sometimes he would surround all the code in one try/catch block
and nest another around the Sql section.

His reasoning is that if he gets an error, he wants to keep the user on a
page and give an error there instead of having it go to some general page.

I was curious about other ideas on this on whether this is a good ideas.
Just trying to get some pros and cons.

Thanks,

Tom
Feb 9 '06 #1
4 1910
Every try/catch has a performance hit. I try to use them sparingly. Users
don't necessarily need extremely detailed information. As a matter of
opinion, I don't want to give them extremely detailed information. If
something failed, such as retrieving records from a db, I want to tell them
that the process failed. I don't want to tell them that the application
couldn't find the database. That's something only I or one of the other
devs/administrators should learn about. You can still keep the user on the
page and have a single try/catch or a minimal number of them. Try/catches
are great for desktop applications, but on web applications they do slow
things up because you could have undreds or thousands of users executing the
page at once, meaning the performance hit by each try/catch is magnified
immensely. Bottom line, I try to discover the best coverage for each
try/catch so that I can trap the error and present information to the user
without going overboard.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
"tshad" <ts**********@f tsolutions.com> wrote in message
news:uG******** ******@TK2MSFTN GP10.phx.gbl...
Normally, I surround my Dataset/fill or DBreader execut with a try/Catch.

Something like:
*************** *************** *************** *********
Dim dbReader As SqlDataReader

Dim ConnectionStrin g as String
=System.Configu ration.Configur ationSettings.A ppSettings("MM_ CONNECTION_STRI NG_Connection")
Dim objConn as New SqlConnection (ConnectionStri ng)
Dim CommandText as String = "Select City,StateCode from zipCodes where
zipCode = @ZipCode and CityType = 'D'"
Dim objCmd as New SqlCommand(Comm andText,objConn )
with objCmd.Paramete rs
.Add("@ZipCode" ,SqlDbType.VarC har,9).value = ZipCode.Text
end with

try
objConn.Open()

dbReader = objCmd.ExecuteR eader

if dbReader.Read then
City.Text = dbReader("City" )
State.Text = dbReader("State Code")
end if
catch ex as Exception
... Do something
finally
objConn.Close()
end try
*************** *************** *************** *************** **

I have someone here that writes his code where he surround all his code
with a try/catch, not just the area where he could logically expect to
have a problem. Sometimes he would surround all the code in one
try/catch block and nest another around the Sql section.

His reasoning is that if he gets an error, he wants to keep the user on a
page and give an error there instead of having it go to some general page.

I was curious about other ideas on this on whether this is a good ideas.
Just trying to get some pros and cons.

Thanks,

Tom

Feb 9 '06 #2
Straight from Mr Hejlsberg himself:
"No, because in a lot of cases, people don't care. They're not going to
handle any of these exceptions. There's a bottom level exception handler
around their message loop. That handler is just going to bring up a dialog
that says what went wrong and continue. The programmers protect their code
by writing try finally's everywhere, so they'll back out correctly if an
exception occurs, but they're not actually interested in handling the
exceptions.

....

In the finally, you protect yourself against the exceptions, but you don't
actually handle them. Error handling you put somewhere else. Surely in any
kind of event-driven application like any kind of modern UI, you typically
put an exception handler around your main message pump, and you just handle
exceptions as they fall out that way. But you make sure you protect yourself
all the way out by deallocating any resources you've grabbed, and so forth.
You clean up after yourself, so you're always in a consistent state. You
don't want a program where in 100 different places you handle exceptions and
pop up error dialogs. What if you want to change the way you put up that
dialog box? That's just terrible. The exception handling should be
centralized, and you should just protect yourself as the exceptions
propagate out to the handler."

Karl
--
http://www.openmymind.net/

"tshad" <ts**********@f tsolutions.com> wrote in message
news:uG******** ******@TK2MSFTN GP10.phx.gbl...
Normally, I surround my Dataset/fill or DBreader execut with a try/Catch.

Something like:
*************** *************** *************** *********
Dim dbReader As SqlDataReader

Dim ConnectionStrin g as String
=System.Configu ration.Configur ationSettings.A ppSettings("MM_ CONNECTION_STRI NG_Connection")
Dim objConn as New SqlConnection (ConnectionStri ng)
Dim CommandText as String = "Select City,StateCode from zipCodes where
zipCode = @ZipCode and CityType = 'D'"
Dim objCmd as New SqlCommand(Comm andText,objConn )
with objCmd.Paramete rs
.Add("@ZipCode" ,SqlDbType.VarC har,9).value = ZipCode.Text
end with

try
objConn.Open()

dbReader = objCmd.ExecuteR eader

if dbReader.Read then
City.Text = dbReader("City" )
State.Text = dbReader("State Code")
end if
catch ex as Exception
... Do something
finally
objConn.Close()
end try
*************** *************** *************** *************** **

I have someone here that writes his code where he surround all his code
with a try/catch, not just the area where he could logically expect to
have a problem. Sometimes he would surround all the code in one
try/catch block and nest another around the Sql section.

His reasoning is that if he gets an error, he wants to keep the user on a
page and give an error there instead of having it go to some general page.

I was curious about other ideas on this on whether this is a good ideas.
Just trying to get some pros and cons.

Thanks,

Tom

Feb 9 '06 #3

you actually want to have many many more

try{}
finally{}

blocks...

instead of

try{}
catch{Exception ex}

blocks.

search for "brad abrams", and you'll find some more info.


"tshad" <ts**********@f tsolutions.com> wrote in message
news:uG******** ******@TK2MSFTN GP10.phx.gbl...
Normally, I surround my Dataset/fill or DBreader execut with a try/Catch.

Something like:
*************** *************** *************** *********
Dim dbReader As SqlDataReader

Dim ConnectionStrin g as String
=System.Configu ration.Configur ationSettings.A ppSettings("MM_ CONNECTION_STRI NG_Connection")
Dim objConn as New SqlConnection (ConnectionStri ng)
Dim CommandText as String = "Select City,StateCode from zipCodes where
zipCode = @ZipCode and CityType = 'D'"
Dim objCmd as New SqlCommand(Comm andText,objConn )
with objCmd.Paramete rs
.Add("@ZipCode" ,SqlDbType.VarC har,9).value = ZipCode.Text
end with

try
objConn.Open()

dbReader = objCmd.ExecuteR eader

if dbReader.Read then
City.Text = dbReader("City" )
State.Text = dbReader("State Code")
end if
catch ex as Exception
... Do something
finally
objConn.Close()
end try
*************** *************** *************** *************** **

I have someone here that writes his code where he surround all his code with
a try/catch, not just the area where he could logically expect to have a
problem. Sometimes he would surround all the code in one try/catch block
and nest another around the Sql section.

His reasoning is that if he gets an error, he wants to keep the user on a
page and give an error there instead of having it go to some general page.

I was curious about other ideas on this on whether this is a good ideas.
Just trying to get some pros and cons.

Thanks,

Tom

tshad wrote: Normally, I surround my Dataset/fill or DBreader execut with a try/Catch.

Something like:
*************** *************** *************** *********
Dim dbReader As SqlDataReader

Dim ConnectionStrin g as String
=System.Configu ration.Configur ationSettings.A ppSettings("MM_ CONNECTION_STRI NG_Connection")
Dim objConn as New SqlConnection (ConnectionStri ng)
Dim CommandText as String = "Select City,StateCode from zipCodes where
zipCode = @ZipCode and CityType = 'D'"
Dim objCmd as New SqlCommand(Comm andText,objConn )
with objCmd.Paramete rs
.Add("@ZipCode" ,SqlDbType.VarC har,9).value = ZipCode.Text
end with

try
objConn.Open()

dbReader = objCmd.ExecuteR eader

if dbReader.Read then
City.Text = dbReader("City" )
State.Text = dbReader("State Code")
end if
catch ex as Exception
... Do something
finally
objConn.Close()
end try
*************** *************** *************** *************** **

I have someone here that writes his code where he surround all his code with
a try/catch, not just the area where he could logically expect to have a
problem. Sometimes he would surround all the code in one try/catch block
and nest another around the Sql section.

His reasoning is that if he gets an error, he wants to keep the user on a
page and give an error there instead of having it go to some general page.

I was curious about other ideas on this on whether this is a good ideas.
Just trying to get some pros and cons.

Thanks,

Tom


Feb 12 '06 #4
<sl***@ipass.ne t> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .

you actually want to have many many more

try{}
finally{}

blocks...

instead of

try{}
catch{Exception ex}

blocks.

search for "brad abrams", and you'll find some more info.
Found 'em.

Helps a lot.

Thanks,

Tom

"tshad" <ts**********@f tsolutions.com> wrote in message
news:uG******** ******@TK2MSFTN GP10.phx.gbl...
Normally, I surround my Dataset/fill or DBreader execut with a try/Catch.

Something like:
*************** *************** *************** *********
Dim dbReader As SqlDataReader

Dim ConnectionStrin g as String
=System.Configu ration.Configur ationSettings.A ppSettings("MM_ CONNECTION_STRI NG_Connection")
Dim objConn as New SqlConnection (ConnectionStri ng)
Dim CommandText as String = "Select City,StateCode from zipCodes where
zipCode = @ZipCode and CityType = 'D'"
Dim objCmd as New SqlCommand(Comm andText,objConn )
with objCmd.Paramete rs
.Add("@ZipCode" ,SqlDbType.VarC har,9).value = ZipCode.Text
end with

try
objConn.Open()

dbReader = objCmd.ExecuteR eader

if dbReader.Read then
City.Text = dbReader("City" )
State.Text = dbReader("State Code")
end if
catch ex as Exception
... Do something
finally
objConn.Close()
end try
*************** *************** *************** *************** **

I have someone here that writes his code where he surround all his code
with
a try/catch, not just the area where he could logically expect to have a
problem. Sometimes he would surround all the code in one try/catch
block
and nest another around the Sql section.

His reasoning is that if he gets an error, he wants to keep the user on a
page and give an error there instead of having it go to some general
page.

I was curious about other ideas on this on whether this is a good ideas.
Just trying to get some pros and cons.

Thanks,

Tom

tshad wrote:
Normally, I surround my Dataset/fill or DBreader execut with a try/Catch.

Something like:
*************** *************** *************** *********
Dim dbReader As SqlDataReader

Dim ConnectionStrin g as String
=System.Configu ration.Configur ationSettings.A ppSettings("MM_ CONNECTION_STRI NG_Connection")
Dim objConn as New SqlConnection (ConnectionStri ng)
Dim CommandText as String = "Select City,StateCode from zipCodes where
zipCode = @ZipCode and CityType = 'D'"
Dim objCmd as New SqlCommand(Comm andText,objConn )
with objCmd.Paramete rs
.Add("@ZipCode" ,SqlDbType.VarC har,9).value = ZipCode.Text
end with

try
objConn.Open()

dbReader = objCmd.ExecuteR eader

if dbReader.Read then
City.Text = dbReader("City" )
State.Text = dbReader("State Code")
end if
catch ex as Exception
... Do something
finally
objConn.Close()
end try
*************** *************** *************** *************** **

I have someone here that writes his code where he surround all his code
with
a try/catch, not just the area where he could logically expect to have a
problem. Sometimes he would surround all the code in one try/catch
block
and nest another around the Sql section.

His reasoning is that if he gets an error, he wants to keep the user on a
page and give an error there instead of having it go to some general
page.

I was curious about other ideas on this on whether this is a good ideas.
Just trying to get some pros and cons.

Thanks,

Tom

Feb 13 '06 #5

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

Similar topics

10
30286
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program, it quitted with core dumped. %test
11
6884
by: kaeli | last post by:
Hey all, I'd like to start using the try/catch construct in some scripts. Older browsers don't support this. What's the best way to test for support for this construct so it doesn't kill non-supporting browsers? TIA -- --
4
4338
by: Abhishek Srivastava | last post by:
Hello All, I have seen code snippets like try { ..... } catch {
11
3474
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
13
3711
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
23
3062
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally block. But, I prefer to dim them "on the fly" only if needed (save as much resources as possible). A little further... I may wish to create a sqlcommand and datareader object ONLY if certain conditions are met. But, if I want to clean these up in the...
32
6114
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I understand Finally runs whether an error was caught or not, I haven't found a use for finally yet.
23
2315
by: pigeonrandle | last post by:
Hi, Does this bit of code represent complete overkill?! try { //create a treenode TreeNode tn = new TreeNode(); //add it to a treeview tv.Nodes.Add(tn);
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8337
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8531
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8628
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7359
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.