Re: Real life cost of using exceptions for control flow?
From: Bruno Jouhier [MVP] (bjouhier_at_club-internet.fr)
Date: 06/11/04
- Next message: Jon Skeet [C# MVP]: "Re: Real life cost of using exceptions for control flow?"
- Previous message: Jonathan Keljo [MS]: "Re: Profiling native images"
- In reply to: Miyra: "Real life cost of using exceptions for control flow?"
- Next in thread: Ice: "Re: Real life cost of using exceptions for control flow?"
- Reply: Ice: "Re: Real life cost of using exceptions for control flow?"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 11 Jun 2004 19:52:35 +0200
"Miyra" <miyra70@yahoo.com> a écrit dans le message de
news:a07d6578.0406101437.439fb7d8@posting.google.com...
> Hi. I'm working with an app that uses exceptions for control flow.
> These are code blocks where exceptions are thrown/caught regularly. A
> couple hundred exceptions occur per hour and they're caught close to
> the point of origination. I'm trying to decide whether to refactor...
>
> What is the cost of throwing an exception in the CLR - relative to,
> say, a conditional statement? Are we taking talking 1+ orders of
> magnitude? Is there significant churn in the CLR? Are my timers and
> threads going to suffer drift or unnecessary swaps each time an
> exception get fired?
I would not worry about the cost of throwing/catching exception (although is
it not negligeable, probably at least 10 times slower than a test). I would
worry more about the design issue behind it.
Exceptions are nice to deal with "exceptional" situations. If you start to
use them to handle "normal" flow of control, you loose on several fronts:
* the "normal" code gets ugly and more difficult to read. You end up
replacing easy-to-understand if-then-else constructs by catch clauses that
discriminate on exception types.
* the code that deals with exceptional cases is difficult to analyze too
because it is polluted by catch clauses that deal with the "normal" logic.
So, for example, it gets difficult to guarantee that all exceptions (the
real ones, that correspond to exceptional cases) are properly logged and
reported to the user.
* program will be slower.
So, rather than worry about the performance of exception, you should worry
about the design choice itself.
>
> Thanks for anyone's input. I've spent time snooping the news groups
> and csharp.net, but haven't run into a great answer yet (besides
> "don't use exceptions for control flow" arguments :-)
>
> -Miyra
>
> ps: Developing with MS Visual C# NET.
>
> ps: Here are some links I found
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/dotnetperftips.asp
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/highperfmanagedapps.asp
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenetchapt15.asp
- Next message: Jon Skeet [C# MVP]: "Re: Real life cost of using exceptions for control flow?"
- Previous message: Jonathan Keljo [MS]: "Re: Profiling native images"
- In reply to: Miyra: "Real life cost of using exceptions for control flow?"
- Next in thread: Ice: "Re: Real life cost of using exceptions for control flow?"
- Reply: Ice: "Re: Real life cost of using exceptions for control flow?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|