I Take Exception To That

and Ned are having a spirited debate over the merits of exception handling.  Oddly
enough, I agree with both of them.


only time I do programming in C++ is to write COM code, and COM and exceptions do
not mix.  The only way to make COM and
C++ exceptions mix is to use smart pointers, and as I’ve already discussed, that only
makes the situation worse by introducing subtle, impossible-to-find-and-debug bugs
in the error handling.  When I write COM
code every function call returns an HRESULT and I carefully ensure that if there is
an error, then everything gets cleaned up properly.  COM
is fundamentally an environment which is based on error return values for error handling.  Any
additional error state can be provided by stateful objects.


the .NET CLR is a completely different environment, one with an exception model built
deep into the runtime itself and into the framework.  You’d
be crazy to not use the exception model
in a fully garbage-collected environment like the CLR.  I
write a lot of C++ and C# code that has to work together via Interop; ALL of my C++
functions returns an error code, and NONE of my C# functions do.  That
is not to say that there is no error handling in my C# code — far from it!  My
code is chock full of try-catch-finally blocks to deal with errors.


languages and object frameworks are tools that afford different styles of
g — attempting to mix the style appropriate to one in another
is a recipe for unmaintainable code.

Tags COM Programming Rants

Comments (5)

You must be logged in to post a comment.

  1. RJI’ve been looking for some information on the performance of Exceptions in the CLR. They always used to be quite expensive in Java (from what my poor memory remembers).Log in to Reply
  2. I would be worried that I was taking a performance hit for something that I could do in another way – not that it *really* matters in the stuff I do, but if there were lots and frequently (apart from having a broken install) I would be peeved.
  3. October 15, 2003 at 2:44 pm
  4. Eric LippertThere are two aspects to exception performance. First, what is the additional over-all burden imposed by exceptions. Second, what’s the cost of throwing an exception?But in the .NET framework, there is no way to “turn off” exceptions. You’re stuck with this infrastructure. If you can’t change it, there’s no reason to worry about it. The question is not “are exceptions fast enough in the CLR?” but rather “is the CLR fast enough?”Log in to Reply
  5. The second concern I don’t care about one bit. Exceptions are, by definition, exceptional. When one of my programs gets an exception, almost always it is either doing something incredibly expensive already (in which case the cost of the throw is irrelevant) or we are going to report the exception to the user (in which case the program is going to come to a screeching halt anyway.) Either way, the per-throw cost is unimportant.
  6. As for the first: exceptions aren’t cheap anywhere. C++ exceptions are quite expensive in fact — take a look at the codegen for the same method with exception handling turned on and off if you want to see what I mean.
  7. October 15, 2003 at 3:59 pm
  8. Siew Moi KhorRJ, you might find Chris Brumme’s excellent post on the exception model useful:
  9. Log in to Reply
  10. October 15, 2003 at 4:00 pm
  11. Ed BallEarly in the process of developing our application (which uses tons of COM), we decided to enclose every non-trivial COM method implementation with a try-catch block, and we used a C-style macro to generate code that catches _com_error, std::bad_alloc, std::exception, and even “…”. The macro logs the exception and returns an appropriate HRESULT. Not pretty, but at least we didn’t have to give up exceptions.
  12. Log in to Reply
  13. October 15, 2003 at 6:47 pm
  14. AnonymousChris Brumme’s article looks very interesting. I think it might be stretching the meaning of the word article a little, I’ll bookmark it and read it when I’ve got a weekend free 🙂
  15. Log in to Reply
  16. October 16, 2003 at 4:00 am

Skip to main content

Follow Us

Popular Tags

C# Scripting JScript VBScript Language Design COM Programming Rarefied Heights Puzzles Rants Performance Security C# 4.0 Non-computer SimpleScript JScript .NET Immutability Code Quality Pages Recursion Books



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s