473,382 Members | 1,348 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,382 software developers and data experts.

Try Catch Question

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

is it good to use the exit function in the catch section in case of some
problem (after writing the error in to some log) ?
Best Regards ,

Tiraman :-)
Nov 20 '05 #1
7 1352
It will not decrease your performance - unless a lot of errors are being
thrown. That is to say, errors should not be thrown unless they are
critical - and they should not be used to control the flow of the program.
If an exception is a rare thing - then the Try/Catch isn't going to decrease
performance. If exceptions are being thrown and caught all over the place to
control the flow (instead of return codes, etc), then yes, you will see
performance degradation.

"Tiraman" <ti*****@netvision.net.il> wrote in message
news:On**************@TK2MSFTNGP10.phx.gbl...
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

is it good to use the exit function in the catch section in case of some
problem (after writing the error in to some log) ?
Best Regards ,

Tiraman :-)

Nov 20 '05 #2
10x marina ,
i m using the try catch only in the places where errors can be thrown like
working with file system , SQL and so on but i m not expecting any error but
for
the unexpected problems which i would like to write them some where in case
that they will appear .

one more question please , what if i have this kind of function

public function A() as something

something = B() ' call the B function

end function

public function B() as something

' some code that handle file system

End Function

Now where should i put the try catch ?

since the B() function can be used any where i think that i should put there
try catch around the file system operations .

but should i put the try catch also in the A() function around the line that
call the B() function ?

Try
something = B()
Catch e as exception
' write the error some where ....
End Try

"Marina" <so*****@nospam.com> wrote in message
news:OV**************@TK2MSFTNGP12.phx.gbl...
It will not decrease your performance - unless a lot of errors are being
thrown. That is to say, errors should not be thrown unless they are
critical - and they should not be used to control the flow of the program.
If an exception is a rare thing - then the Try/Catch isn't going to decrease performance. If exceptions are being thrown and caught all over the place to control the flow (instead of return codes, etc), then yes, you will see
performance degradation.

"Tiraman" <ti*****@netvision.net.il> wrote in message
news:On**************@TK2MSFTNGP10.phx.gbl...
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

is it good to use the exit function in the catch section in case of some problem (after writing the error in to some log) ?
Best Regards ,

Tiraman :-)


Nov 20 '05 #3
That really depends on the overall design, and what your needs are, as well
as what you need to happen in the case of this error.

Let's say B returns True if a database connection was successful and False
otherwise. Well, if the fact that the connection was not successfuly means
your whole program cannot function - then you need to know this at the very
highest level. So you may want to throw the exception up, and only have a
try/catch in the main function. This would then signal the error to the user
and close the program.

Alternatively, maybe it's not the end of the world - and maybe you want the
user to re-enter the database connection info. In this case, you would want
the try/catch in the B function and have it return False. A would then
prompt the user to re-enter the connection information.

I don't think there is one answer, it all depends on the consumers of your
function and the specific situation.

"Tiraman" <ti*****@netvision.net.il> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
10x marina ,
i m using the try catch only in the places where errors can be thrown like
working with file system , SQL and so on but i m not expecting any error but for
the unexpected problems which i would like to write them some where in case that they will appear .

one more question please , what if i have this kind of function

public function A() as something

something = B() ' call the B function

end function

public function B() as something

' some code that handle file system

End Function

Now where should i put the try catch ?

since the B() function can be used any where i think that i should put there try catch around the file system operations .

but should i put the try catch also in the A() function around the line that call the B() function ?

Try
something = B()
Catch e as exception
' write the error some where ....
End Try

"Marina" <so*****@nospam.com> wrote in message
news:OV**************@TK2MSFTNGP12.phx.gbl...
It will not decrease your performance - unless a lot of errors are being
thrown. That is to say, errors should not be thrown unless they are
critical - and they should not be used to control the flow of the program.
If an exception is a rare thing - then the Try/Catch isn't going to decrease
performance. If exceptions are being thrown and caught all over the

place to
control the flow (instead of return codes, etc), then yes, you will see
performance degradation.

"Tiraman" <ti*****@netvision.net.il> wrote in message
news:On**************@TK2MSFTNGP10.phx.gbl...
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

is it good to use the exit function in the catch section in case of

some problem (after writing the error in to some log) ?
Best Regards ,

Tiraman :-)



Nov 20 '05 #4
i think that i got the idea

10x 4 your help .

"Marina" <so*****@nospam.com> wrote in message
news:Ol**************@TK2MSFTNGP09.phx.gbl...
That really depends on the overall design, and what your needs are, as well as what you need to happen in the case of this error.

Let's say B returns True if a database connection was successful and False
otherwise. Well, if the fact that the connection was not successfuly means your whole program cannot function - then you need to know this at the very highest level. So you may want to throw the exception up, and only have a
try/catch in the main function. This would then signal the error to the user and close the program.

Alternatively, maybe it's not the end of the world - and maybe you want the user to re-enter the database connection info. In this case, you would want the try/catch in the B function and have it return False. A would then
prompt the user to re-enter the connection information.

I don't think there is one answer, it all depends on the consumers of your
function and the specific situation.

"Tiraman" <ti*****@netvision.net.il> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
10x marina ,
i m using the try catch only in the places where errors can be thrown like
working with file system , SQL and so on but i m not expecting any error

but
for
the unexpected problems which i would like to write them some where in

case
that they will appear .

one more question please , what if i have this kind of function

public function A() as something

something = B() ' call the B function

end function

public function B() as something

' some code that handle file system

End Function

Now where should i put the try catch ?

since the B() function can be used any where i think that i should put

there
try catch around the file system operations .

but should i put the try catch also in the A() function around the line

that
call the B() function ?

Try
something = B()
Catch e as exception
' write the error some where ....
End Try

"Marina" <so*****@nospam.com> wrote in message
news:OV**************@TK2MSFTNGP12.phx.gbl...
It will not decrease your performance - unless a lot of errors are being thrown. That is to say, errors should not be thrown unless they are
critical - and they should not be used to control the flow of the

program. If an exception is a rare thing - then the Try/Catch isn't going to

decrease
performance. If exceptions are being thrown and caught all over the

place
to
control the flow (instead of return codes, etc), then yes, you will see performance degradation.

"Tiraman" <ti*****@netvision.net.il> wrote in message
news:On**************@TK2MSFTNGP10.phx.gbl...
> 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
>
> is it good to use the exit function in the catch section in case of

some
> problem (after writing the error in to some log) ?
>
>
> Best Regards ,
>
> Tiraman :-)
>
>



Nov 20 '05 #5
Tiraman,
I am using allot the try catch in my code and the question is
if it is good ? I would favor global exception handlers over a lot of Try/Catch statements,
however I have a number of Try/Finally statements to ensure resources are
disposed of properly. In other words: Try/Finally good, Try/Catch not so
good. See MSDN article below.
it will decrease my performance ? A Try statement does not decrease performance, the act of throwing &
handling an exception will. In other words if you never throw an exception
there is no performance impact.
is it good to use the exit function in the catch section in case of some
problem (after writing the error in to some log) ? If I had a problem in a Catch block, I would allow an exception to be raised
that would be handled by a higher catch block or one of my global exception
handlers.

Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when you
use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

For example: In my Windows Forms apps I would have a handler attached to the
Application.ThreadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the MainForm
raises an exception, the Application.ThreadException handler will catch all
uncaught exceptions from any form/control event handlers.

Hope this helps
Jay
"Tiraman" <ti*****@netvision.net.il> wrote in message
news:On**************@TK2MSFTNGP10.phx.gbl... 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

is it good to use the exit function in the catch section in case of some
problem (after writing the error in to some log) ?
Best Regards ,

Tiraman :-)

Nov 20 '05 #6
Hi Marina,

I find this a very nice sentence from you, I hope you do not mind that I
will use it in future. It describes exactly as I think it has to be.
--------------
That is to say, errors should not be thrown unless they are critical - and
they should not be used to control the flow of the program.
--------------
Cor

Nov 20 '05 #7
Hi Jay ,

10x for your help ,
i will go over the article and if i will have any question i will ask :-)

bye

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uQ**************@TK2MSFTNGP12.phx.gbl...
Tiraman,
I am using allot the try catch in my code and the question is
if it is good ? I would favor global exception handlers over a lot of Try/Catch

statements, however I have a number of Try/Finally statements to ensure resources are
disposed of properly. In other words: Try/Finally good, Try/Catch not so
good. See MSDN article below.
it will decrease my performance ? A Try statement does not decrease performance, the act of throwing &
handling an exception will. In other words if you never throw an exception
there is no performance impact.
is it good to use the exit function in the catch section in case of some problem (after writing the error in to some log) ?

If I had a problem in a Catch block, I would allow an exception to be

raised that would be handled by a higher catch block or one of my global exception handlers.

Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when you use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

For example: In my Windows Forms apps I would have a handler attached to the Application.ThreadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the MainForm raises an exception, the Application.ThreadException handler will catch all uncaught exceptions from any form/control event handlers.

Hope this helps
Jay
"Tiraman" <ti*****@netvision.net.il> wrote in message
news:On**************@TK2MSFTNGP10.phx.gbl...
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

is it good to use the exit function in the catch section in case of some problem (after writing the error in to some log) ?
Best Regards ,

Tiraman :-)


Nov 20 '05 #8

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,...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.