By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 1,875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Try Catch Question

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.