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

Try Catch End Try - Performance issue

I really like this function but have tried to slow down
on using it because I get a 1 second pause each time I
use it. I don't really understand why the computer has
to think for 1 second! Especially at 2.8 GZ and 1GB RAM.

The same pause happens on different situations: database
access Trys, Array access Trys. Hummm??
Nov 20 '05 #1
9 1624
there's not a hit for just wrapping the same code in try/catch. Are you in
debug mode by any chance?
"Bob Achgill" <an*******@discussions.microsoft.com> wrote in message
news:b8****************************@phx.gbl...
I really like this function but have tried to slow down
on using it because I get a 1 second pause each time I
use it. I don't really understand why the computer has
to think for 1 second! Especially at 2.8 GZ and 1GB RAM.

The same pause happens on different situations: database
access Trys, Array access Trys. Hummm??

Nov 20 '05 #2
In article <b8****************************@phx.gbl>, Bob Achgill wrote:
I really like this function but have tried to slow down
on using it because I get a 1 second pause each time I
use it. I don't really understand why the computer has
to think for 1 second! Especially at 2.8 GZ and 1GB RAM.

The same pause happens on different situations: database
access Trys, Array access Trys. Hummm??


The construct try/catch/finally introduces very little overhead to your
program - but with a catch ;). If you actaully catch an exception, then
you pay a price. That's why exceptions should be, well - exeptional :)
In other words, it is always good practice to code defensively to avoid
having any exceptions thrown in the first place - especially in a tight
loop or a flow controls consturct of some type. That means, that
methods that you create should probably NOT throw execptions on failure,
but instead return a failure result of some sort - like a Boolean value.

I know I alluded to this in the above, but I think it bears repeating...
Don't use exceptions for flow control of your program. Throwing and
Catching exceptions is very costly. So, DO use Try/Catch/Finally - Do
your best not to throw any exceptions :)

--
Tom Shelton [MVP]
Nov 20 '05 #3
Hi Tom

Can you explain why the first exception that is thrown takes a significant
time to execute (several seconds), whilst subsequent exceptions take very
little time?

Whilst I understand what you say about not throwing exceptions for the sake
of it, it offers a convenient third way. I like to think in terms of a
function taking parameters and returning a result, and failure being flagged
by an exception. Why else do we have the InvalidArgumentException exception
(or whatever it's called)? Isn't that just the kind of exception that could
be thrown all the time, if the user insists on entering 99 when the maximum
value permitted is 98?

How about timeouts? Timeouts with a retry could quite reasonably be
implemented with exceptions, but if there is an overhead then the whole
thing becomes sluggish. If the overhead is caused by the collection of the
stack trace, then I would prefer the default to be to omit the stack trace,
and allow me to specify when it is to be collected. If not, I wonder what it
is that takes all the time.

Charles
"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:OD**************@TK2MSFTNGP09.phx.gbl...
In article <b8****************************@phx.gbl>, Bob Achgill wrote:
I really like this function but have tried to slow down
on using it because I get a 1 second pause each time I
use it. I don't really understand why the computer has
to think for 1 second! Especially at 2.8 GZ and 1GB RAM.

The same pause happens on different situations: database
access Trys, Array access Trys. Hummm??


The construct try/catch/finally introduces very little overhead to your
program - but with a catch ;). If you actaully catch an exception, then
you pay a price. That's why exceptions should be, well - exeptional :)
In other words, it is always good practice to code defensively to avoid
having any exceptions thrown in the first place - especially in a tight
loop or a flow controls consturct of some type. That means, that
methods that you create should probably NOT throw execptions on failure,
but instead return a failure result of some sort - like a Boolean value.

I know I alluded to this in the above, but I think it bears repeating...
Don't use exceptions for flow control of your program. Throwing and
Catching exceptions is very costly. So, DO use Try/Catch/Finally - Do
your best not to throw any exceptions :)

--
Tom Shelton [MVP]

Nov 20 '05 #4
Charles Law wrote:
Whilst I understand what you say about not throwing exceptions for
the sake of it, it offers a convenient third way. I like to think in
terms of a function taking parameters and returning a result, and
failure being flagged by an exception. Why else do we have the
InvalidArgumentException exception (or whatever it's called)? Isn't
that just the kind of exception that could be thrown all the time, if
the user insists on entering 99 when the maximum value permitted is
98?


No, at least in my opinion, that's not what InvalidArgumentException is for.

My opinion on it is this: standard C++ library exceptions are split in two
general "types" of exceptions, the std::logic_error, and the
std::runtime_error (both inherit from std::exception). Although .Net doesn't
have this distinction, I still like to think of exceptions in this way. A
std::logic_error is a programmer's mistake, an exception that should not
occur in a correctly programmed application, regardless of user input. A
std::runtime_error is an error that can occur at runtime and isn't caused by
the programmer (think files that fail to open etc.)

In C++, the std::invalid_argument class inherits from std::logic_error.
That's also how I see InvalidArgumentException. It's meant to be an
exception that indicates that the *programmer* is passing an invalid value
into a function. Therefore, if you are asking user input, and there is a
reasonable way to check if the value is correct *before* passing it to a
function that would throw an InvalidArgumentException if it's not correct,
you should attempt to do so.

This is of course not a law or anything. It's just the rule that I tend to
follow.

Also, any overhead from the first exception being thrown is probably caused
by the relevant exception throwing/handling code being jit-compiled. When
that's done, the subsequent exceptions will be cheaper to throw.

--
Sven Groot

http://unforgiven.bloghorn.com

Nov 20 '05 #5
Inline..

"Charles Law" <bl***@nowhere.com> wrote in message
news:OQ**************@TK2MSFTNGP09.phx.gbl...
Hi Tom

Can you explain why the first exception that is thrown takes a significant
time to execute (several seconds), whilst subsequent exceptions take very
little time?
A number of things happen. First of all, there's always a slight delay
because the exception can be trying to capture the current stack frame. As
for first-time delays, one of the biggest culprits is that most of the core
exception messages are stored as localized resource strings, and the
resource manager has to make sure the correct resource is loaded (could have
to load a DLL) and then locate the text. After that, things are probably
cached.
Whilst I understand what you say about not throwing exceptions for the sake of it, it offers a convenient third way. I like to think in terms of a
function taking parameters and returning a result, and failure being flagged by an exception. Why else do we have the InvalidArgumentException exception (or whatever it's called)? Isn't that just the kind of exception that could be thrown all the time, if the user insists on entering 99 when the maximum value permitted is 98?
There's nothing wrong with throwing InvalidArgumentException, but the
calling code (the one with the try-catch) should NEVER depend on an error
for logic branching. Catching exceptions should be... well, an exceptional
case that only happens on a small number of exceptional occasions. The
calling code, if it needs to perform, should make sure it doesn't do
something that could normally cause an exception to take place. Exceptions
should only happen once in a blue moon if all is well (relatively speaking).
Exceptions should not be a lazy coder's alternative to checking things prior
to executing something.

An extreme example of this is the DirectX library for .NET. Most of the
function calls in the .NET library call some unmanaged DirectX code, and
they translate the simple error numbers from the C++ API into .NET
exceptions. But in some cases, like the Render methods that get called
dozens of times (at least) per second, and have a high chance of failing
simply pass the error number back from the method instead of raising the
exception.
How about timeouts? Timeouts with a retry could quite reasonably be
implemented with exceptions, but if there is an overhead then the whole
thing becomes sluggish. If the overhead is caused by the collection of the
stack trace, then I would prefer the default to be to omit the stack trace, and allow me to specify when it is to be collected. If not, I wonder what it is that takes all the time.
This is fine, but timeouts shouldn't happen during the normal course of
operation. If they happen, something went wrong (and took an unusually long
time to finish). You shouldn't be catching timeout exceptions 20 times a
second, which is where you'd have perf problems :-)

-Rob Teixeira [MVP]
Charles
"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:OD**************@TK2MSFTNGP09.phx.gbl...
In article <b8****************************@phx.gbl>, Bob Achgill wrote:
I really like this function but have tried to slow down
on using it because I get a 1 second pause each time I
use it. I don't really understand why the computer has
to think for 1 second! Especially at 2.8 GZ and 1GB RAM.

The same pause happens on different situations: database
access Trys, Array access Trys. Hummm??


The construct try/catch/finally introduces very little overhead to your
program - but with a catch ;). If you actaully catch an exception, then
you pay a price. That's why exceptions should be, well - exeptional :)
In other words, it is always good practice to code defensively to avoid
having any exceptions thrown in the first place - especially in a tight
loop or a flow controls consturct of some type. That means, that
methods that you create should probably NOT throw execptions on failure,
but instead return a failure result of some sort - like a Boolean value.

I know I alluded to this in the above, but I think it bears repeating...
Don't use exceptions for flow control of your program. Throwing and
Catching exceptions is very costly. So, DO use Try/Catch/Finally - Do
your best not to throw any exceptions :)

--
Tom Shelton [MVP]


Nov 20 '05 #6
Hi Rob

Points taken. In fact, what I tend to do as I am nearing the end of a
development is set all exceptions to break into the debugger when they
occur, so that I can see immediately if I am getting a lot/any in normal
operation.

Charles
"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Inline..

"Charles Law" <bl***@nowhere.com> wrote in message
news:OQ**************@TK2MSFTNGP09.phx.gbl...
Hi Tom

Can you explain why the first exception that is thrown takes a significant time to execute (several seconds), whilst subsequent exceptions take very little time?
A number of things happen. First of all, there's always a slight delay
because the exception can be trying to capture the current stack frame. As
for first-time delays, one of the biggest culprits is that most of the

core exception messages are stored as localized resource strings, and the
resource manager has to make sure the correct resource is loaded (could have to load a DLL) and then locate the text. After that, things are probably
cached.
Whilst I understand what you say about not throwing exceptions for the sake
of it, it offers a convenient third way. I like to think in terms of a
function taking parameters and returning a result, and failure being

flagged
by an exception. Why else do we have the InvalidArgumentException

exception
(or whatever it's called)? Isn't that just the kind of exception that

could
be thrown all the time, if the user insists on entering 99 when the

maximum
value permitted is 98?


There's nothing wrong with throwing InvalidArgumentException, but the
calling code (the one with the try-catch) should NEVER depend on an error
for logic branching. Catching exceptions should be... well, an exceptional
case that only happens on a small number of exceptional occasions. The
calling code, if it needs to perform, should make sure it doesn't do
something that could normally cause an exception to take place. Exceptions
should only happen once in a blue moon if all is well (relatively

speaking). Exceptions should not be a lazy coder's alternative to checking things prior to executing something.

An extreme example of this is the DirectX library for .NET. Most of the
function calls in the .NET library call some unmanaged DirectX code, and
they translate the simple error numbers from the C++ API into .NET
exceptions. But in some cases, like the Render methods that get called
dozens of times (at least) per second, and have a high chance of failing
simply pass the error number back from the method instead of raising the
exception.
How about timeouts? Timeouts with a retry could quite reasonably be
implemented with exceptions, but if there is an overhead then the whole
thing becomes sluggish. If the overhead is caused by the collection of the stack trace, then I would prefer the default to be to omit the stack trace,
and allow me to specify when it is to be collected. If not, I wonder

what it
is that takes all the time.
This is fine, but timeouts shouldn't happen during the normal course of
operation. If they happen, something went wrong (and took an unusually

long time to finish). You shouldn't be catching timeout exceptions 20 times a
second, which is where you'd have perf problems :-)

-Rob Teixeira [MVP]
Charles
"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:OD**************@TK2MSFTNGP09.phx.gbl...
In article <b8****************************@phx.gbl>, Bob Achgill wrote: > I really like this function but have tried to slow down
> on using it because I get a 1 second pause each time I
> use it. I don't really understand why the computer has
> to think for 1 second! Especially at 2.8 GZ and 1GB RAM.
>
> The same pause happens on different situations: database
> access Trys, Array access Trys. Hummm??

The construct try/catch/finally introduces very little overhead to your program - but with a catch ;). If you actaully catch an exception, then you pay a price. That's why exceptions should be, well - exeptional :) In other words, it is always good practice to code defensively to avoid having any exceptions thrown in the first place - especially in a tight loop or a flow controls consturct of some type. That means, that
methods that you create should probably NOT throw execptions on failure, but instead return a failure result of some sort - like a Boolean value.
I know I alluded to this in the above, but I think it bears repeating... Don't use exceptions for flow control of your program. Throwing and
Catching exceptions is very costly. So, DO use Try/Catch/Finally - Do
your best not to throw any exceptions :)

--
Tom Shelton [MVP]



Nov 20 '05 #7
Hi Sven

Regarding the JITting and caching, I would have thought that, as I am in the
debugger, the caching would occur once for the period that I was in the IDE.
It appears though that it occurs once per compile/run session.

Oh well, I am sure I shall have to live with it ;-)

Charles
"Sven Groot" <sv*******@gmx.net> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
Charles Law wrote:
Whilst I understand what you say about not throwing exceptions for
the sake of it, it offers a convenient third way. I like to think in
terms of a function taking parameters and returning a result, and
failure being flagged by an exception. Why else do we have the
InvalidArgumentException exception (or whatever it's called)? Isn't
that just the kind of exception that could be thrown all the time, if
the user insists on entering 99 when the maximum value permitted is
98?
No, at least in my opinion, that's not what InvalidArgumentException is

for.
My opinion on it is this: standard C++ library exceptions are split in two
general "types" of exceptions, the std::logic_error, and the
std::runtime_error (both inherit from std::exception). Although .Net doesn't have this distinction, I still like to think of exceptions in this way. A
std::logic_error is a programmer's mistake, an exception that should not
occur in a correctly programmed application, regardless of user input. A
std::runtime_error is an error that can occur at runtime and isn't caused by the programmer (think files that fail to open etc.)

In C++, the std::invalid_argument class inherits from std::logic_error.
That's also how I see InvalidArgumentException. It's meant to be an
exception that indicates that the *programmer* is passing an invalid value
into a function. Therefore, if you are asking user input, and there is a
reasonable way to check if the value is correct *before* passing it to a
function that would throw an InvalidArgumentException if it's not correct,
you should attempt to do so.

This is of course not a law or anything. It's just the rule that I tend to
follow.

Also, any overhead from the first exception being thrown is probably caused by the relevant exception throwing/handling code being jit-compiled. When
that's done, the subsequent exceptions will be cheaper to throw.

--
Sven Groot

http://unforgiven.bloghorn.com

Nov 20 '05 #8
Hi Rob,

Nice message,

Cor
Nov 20 '05 #9
Hi Tom,
Don't use exceptions for flow control of your program. Throwing and
Catching exceptions is very costly. So, DO use Try/Catch/Finally - Do
your best not to throw any exceptions :)


I have the same idea as you, I write this because I find this statement
important.
(And have nothing to add, however want to say that)

Cor
Nov 20 '05 #10

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

Similar topics

6
by: Erik Cruz | last post by:
Hi. I have read several articles recommending avoid to raise exceptions when possible, since exceptions are expensive to the system. Removing code from Try... Catch blocks can help performance?...
24
by: Steven T. Hatton | last post by:
If I understand correctly, I have no assurance that I can determine the type of a simple class instance thrown as an exception unless I explicitly catch it by name. (non-derived classes having no...
1
by: Greg Merideth | last post by:
I have a custom service running under win2k3 that grabs performance data and stores it into a sql database. I've noticed on occasion when the data gathering stops, using the sysinternals process...
11
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) {}
37
by: clintonG | last post by:
Has somebody written any guidelines regarding how to determine when try-catch blocks should be used and where their use would or could be considered superfluous? <%= Clinton Gallagher...
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...
6
by: john_c | last post by:
FxCopy says this about catching general exceptions: "You should not catch Exception or SystemException. Catching generic exception types can hide run-time problems from the library user, and can...
9
by: =?Utf-8?B?TWlrZQ==?= | last post by:
Hi. I have a class with an collection property. The collection property is sometimes equal to nothing. The class has a function named 'Exists' which returns TRUE if the specified string exists...
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
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...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.