This is the archived version of Roland Weigelt's weblog that ran from 2003 to 2023 at weblogs.asp.net

Why throwing an exception should be the exception

Alex writes about the impact of throwing an exception on performance and that it may not be as bad as one may assume. In the comments, people write that using exceptions for flow control is not the Right Thing to do.

Here’s my #1 reason why throwing an exception for flow control is definitely not a good idea: Debugging. When I’m debugging and I’ve set the debugger to “break on CLR exceptions” (and no, I don’t want to get more specific when I switch that option on and off dozens of times per day), I don’t want code execution to halt over and over again until I get to the actual problem.

It’s bad enough if some exceptions in non-exceptional situations cannot be avoided — I don’t need to introduce them in my own code.

1 Comment

  • The cause of the fact that debugging is hard to do is just because you don't have a way to see what kind of exceptions you can expect.

    Normally you'd write unit test, which tests all kind of expected exceptions, this makes you understand the code a lot better. And at the same time shows up bad design in the underlying code.

Comments have been disabled for this content.