G
Guest
Good points Dave.
I will add that I think saying:
"Exceptions are epxensive"
is practically equivalent to saying:
"Eating at McDonalds will make you fat"
Sure if I eat at McDonalds every day, 5 times day then I am going to end up
fat, but if I am just eating there once a week or once a month then there is
no harm (disregarding food poisoning ). The same goes for exceptions, if
they are occuring infrequently then there is no real performance issue,
however if your design is throwing thousands of exceptions in normal
operation you should rethink it.
It is a lot better in my opinion to throw an exception to alert of a
condition than rely on error codes in most cases, an exception is going to
make sure that the error case is handled by the calling programmer or the app
terminates, minimizing data loss, unlike error codes which do ot have to be
checked.
Mark.
--
http://www.markdawson.org
I will add that I think saying:
"Exceptions are epxensive"
is practically equivalent to saying:
"Eating at McDonalds will make you fat"
Sure if I eat at McDonalds every day, 5 times day then I am going to end up
fat, but if I am just eating there once a week or once a month then there is
no harm (disregarding food poisoning ). The same goes for exceptions, if
they are occuring infrequently then there is no real performance issue,
however if your design is throwing thousands of exceptions in normal
operation you should rethink it.
It is a lot better in my opinion to throw an exception to alert of a
condition than rely on error codes in most cases, an exception is going to
make sure that the error case is handled by the calling programmer or the app
terminates, minimizing data loss, unlike error codes which do ot have to be
checked.
Mark.
--
http://www.markdawson.org
Dave Sexton said:Hi Bill,
You don't really need experts here. My testing showed 18 exceptions per
millisecond on Vista Beta 2 and 17 on Windows XP (one hyperthreaded CPU;
same machine for both tests; .NET 2.0) using Jon Skeet's program [1]
(already cited in this thread by David Browne). Here is a conversation
discussing my tests results, in its original context:
http://groups.google.com/group/micr...m"+author:dave+sexton&rnum=1#5518b8e9442b4113
If a program throws a single exception, even for input validation (Bruce
Wood mentioned in this thread that he'll use return codes instead), the
performance hit pales in comparison to any of the other consequently
slow-performing design choices you see riddled in common code, such as not
coding long-running processes to run on a background thread, for example.
In other words, performance should probably be the least of a developer's
concern when determining whether an exception should be thrown.
The problem, to paraphrase Jon Skeet, is that a stigma has become of
throwing exceptions so much that people adamantly recommend to avoid
throwing them, probably due to testing in a debugger (Jon's article points
out the difference in performance with and without a debugger attached);
however, this comes with a price by making code that much harder to debug -
debugging is the point of having an exception with a message, stack trace
and corresponding error data in the first place.
I'd say, instead of recommending that exceptions be avoided, recommend that
exceptions should be used where appropriate and that developers should
rethink their design only after seeing any unacceptable performance
degradation in a program while testing (without a debugger attached).
Naturally, as bugs are being fixed, less and less exceptions will be thrown
anyway. But if simply throwing exceptions is causing a program to run
slowly, then the author will probably want to rethink the entire
architecture since any program that is throwing 17000 exceptions per second
has some major architectural flaw, IMO
Anyway, did the MS people ever get back to you with their opinions on this
matter?
--
Dave Sexton
[1] J. Skeet, Exceptions and Performance
http://www.yoda.arachsys.com/csharp/exceptions.html
William (Bill) Vaughn said:Tell, ya what. I'll recheck with the framework people on campus that told
me to avoid exceptions--you know, the people that wrote the code. My tests
clearly show that throwing an exception is very expensive in the framework
but I could be wrong. Let's ask another expert...
--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no
rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)