473,325 Members | 2,860 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,325 software developers and data experts.

Try/Catch question

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

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

Dim ConnectionString as String
=System.Configuration.ConfigurationSettings.AppSet tings("MM_CONNECTION_STRING_Connection")
Dim objConn as New SqlConnection (ConnectionString)
Dim CommandText as String = "Select City,StateCode from zipCodes where
zipCode = @ZipCode and CityType = 'D'"
Dim objCmd as New SqlCommand(CommandText,objConn)
with objCmd.Parameters
.Add("@ZipCode",SqlDbType.VarChar,9).value = ZipCode.Text
end with

try
objConn.Open()

dbReader = objCmd.ExecuteReader

if dbReader.Read then
City.Text = dbReader("City")
State.Text = dbReader("StateCode")
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
8 1254
This is really a matter of application structure. If you want the user
to go somewhere else, then your way is better. However, you may find
that it will depend on the actual location/page/form your user is in
that will determine what action you want taken. Most of my applications
have both scenarios, and even include some functions that have no
try/catch in them at all because I want to handle errors from there
outside the function at all times.

Hope this helps.

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

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

Dim ConnectionString as String
=System.Configuration.ConfigurationSettings.AppSe ttings("MM_CONNECTION_STRING_Connection")
Dim objConn as New SqlConnection (ConnectionString)
Dim CommandText as String = "Select City,StateCode from zipCodes where
zipCode = @ZipCode and CityType = 'D'"
Dim objCmd as New SqlCommand(CommandText,objConn)
with objCmd.Parameters
.Add("@ZipCode",SqlDbType.VarChar,9).value = ZipCode.Text
end with

try
objConn.Open()

dbReader = objCmd.ExecuteReader

if dbReader.Read then
City.Text = dbReader("City")
State.Text = dbReader("StateCode")
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
Hello tshad,
Normally, I surround my Dataset/fill or DBreader execut with a
try/Catch.
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.


It really depends on what you are planning on doing with the exception information.
It is possible to let the exception bubble out of the data access method
(assuming it isn't just included in the Form_Load event, but encapsulated
elsewhere). For instance, you could just to the Try..Finally and dispose
of your connection in this method. The failure exception will bubble up to
the UI layer where you would handle it as necessary.

BTW, in your instance, you are just closing the connection. I understand
you need to dispose it as well to avoid memory leaks. If you are using 2.0,
you should use the Using keyword with your connection and data reader to
make sure they are disposed properly.

As for what to do with the error, it depends on whether the user can do anything
with the exception information. Don't simply post the exception information
to the end user. Give them viable options to fix the problem or notify them
that there is a problem and give them a way to contact someone for assistance.
If nothing else, you should log it somehow, otherwise you can get exceptions
and never know your code is failing.

Jim Wooley
Feb 9 '06 #3
CMM
I would certainly not wrap EVERY method in a Try/Catch. This harkens back to
VB.Classic OnError model and the fact that an unhandled error would force
your app to blow up. This is no longer true in .NET (unless it happens like
during app startup).

However, (in ASP.NET specifically) sometimes you do want to wrap an entire
algorithm in a TryCatch block.... if only because in that *particular
instance* you don't want the user redirected to your generic error.htm (if
you've set one up... or the ugly unprofessional default ASP error page
otherwise). Like the others replied... it depends on the situation. But, as
a "general practice," the answer is absolutely not.
Feb 9 '06 #4
Jim,
BTW, in your instance, you are just closing the connection. I understand
you need to dispose it as well to avoid memory leaks.


As often showed is this something from the past were some people probably
where mixing up finalize with dispose.

There is in the case of a connection not any advantage above disposing and
closing. Disposing takes only some extra instructions by instance with
removing the connectionstring from the connection object.

Cor
Feb 9 '06 #5
Tom,

It is as you wish, however if you want to do it more global, than is in my
idea the global exception handler better.

It is often handled by Jay in this newsgroup. Have by instance a look at
this message thread.

http://groups.google.com/group/micro...0b7be6bb0cf917
I hope this helps,

Cor
Feb 9 '06 #6
CMM
I got the impression that he was talking about ASP.NET (he mentioned
"pages")... in which case the UI ThreadException handler wouldn't be of
use... he would use the Application_Error event in Global.asax instead.

Personally, I usually just defer to customErrors - defaultRedirect in
Web.config. It depends on the requirements of the Web Application (usually a
"logging" requirement concern).

--
-C. Moya
www.cmoya.com
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:OM**************@TK2MSFTNGP11.phx.gbl...
Tom,

It is as you wish, however if you want to do it more global, than is in my
idea the global exception handler better.

It is often handled by Jay in this newsgroup. Have by instance a look at
this message thread.

http://groups.google.com/group/micro...0b7be6bb0cf917
I hope this helps,

Cor

Feb 9 '06 #7

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:OM**************@TK2MSFTNGP11.phx.gbl...
Tom,

It is as you wish, however if you want to do it more global, than is in my
idea the global exception handler better.

It is often handled by Jay in this newsgroup. Have by instance a look at
this message thread.

http://groups.google.com/group/micro...0b7be6bb0cf917
I hope this helps,
It does.

I got a lot of good ideas and information from everyone here.

Thanks,

Tom
Cor

Feb 14 '06 #8
"CMM" <cm*@nospam.com> wrote in message
news:uo*************@TK2MSFTNGP12.phx.gbl...
I got the impression that he was talking about ASP.NET (he mentioned
"pages")... in which case the UI ThreadException handler wouldn't be of
use... he would use the Application_Error event in Global.asax instead.
I was and am using the Application_Error event.

Personally, I usually just defer to customErrors - defaultRedirect in
Web.config. It depends on the requirements of the Web Application (usually
a "logging" requirement concern).

This was something I was curious about. When would I use the Web.Config
redirect (CustomErrors) and when would I use Application_Error?

Can I use them both and if you can which one takes preference?

Thanks,

Tom --
-C. Moya
www.cmoya.com
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:OM**************@TK2MSFTNGP11.phx.gbl...
Tom,

It is as you wish, however if you want to do it more global, than is in
my idea the global exception handler better.

It is often handled by Jay in this newsgroup. Have by instance a look at
this message thread.

http://groups.google.com/group/micro...0b7be6bb0cf917
I hope this helps,

Cor


Feb 14 '06 #9

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

Similar topics

5
by: Jacek Dziedzic | last post by:
Hi! In my main() function I have a last-resort exception construct that looks like this: int main() { try { // ... program code }
13
by: Woody Splawn | last post by:
I have a try catch statement in a fucntion that is supposed to return a true or a false My code looks like this: Try mySqlConnection.Open() Dim Da1 As New SqlDataAdapter("Select JnlType,...
7
by: Tiraman | last post by:
Hi , I am using allot the try catch in my code and the question is if it is good ? it will decrease my performance ? one more question
9
by: Michael MacDonald | last post by:
Does someone have a good site I can visit or explain the use of Try" and Catch foe exception/error handling. What is the logic behind this command and maybe an example would be great!!!! Mike_Mac...
2
by: Keith Kowalski | last post by:
I anm opening up a text file reading the lines of the file that refer to a tif image in that file, If the tif image does not exist I need it to send an email stating that the file doesn't exist...
5
by: Patrick Dickey | last post by:
Hello, All! I'm modifying some source code that I downloaded from Planet-source-code a while back, and have a question about Try Catch statements. Basically, I'm wondering if I can do a Try with...
32
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...
23
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);
6
by: rhaazy | last post by:
I am looking for some feedback on using try catch statements. Usually when I start a project I use them for everything, but stop using them as often after the "meat n' potatos" of the project is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.