Skip to content Skip to sidebar Skip to footer

Catching Exceptions As Expected Program Execution Flow Control?

I always felt that expecting exceptions to be thrown on a regular basis and using them as flow logic was a bad thing. Exceptions feel like they should be, well, the 'exception'.

Solution 1:

Your lead is absolutely right. Exceptions are not just for once in a blue moon situations, but specifically for reporting other than expected outcomes.

In this case the foreign key check would still take place, and exceptions are the mechanism by which you can be notified.

What you should NOT do is catch and suppress exceptions with a blanket catchall statement. Doing fine-grained exception handling is specifically why exceptions were designed in the first place.

Solution 2:

Wow,

First off, can you please distill the question down a bit, while it was nice to read a well thought out and explained question, that was quite a lot to digest.

The short answer is "yes", but it can depend.

  • We have some applications where we have lots of business logic tied up in the SQL queries (not my design Gov!). If this is how it is structured, management can be difficult to convince of otherwise since it "already works".
  • In this situation, does it really make a big deal? Since it's still one trip across the wire and back. Does the server do much before it realises that it cannot continue (i.e.if there is a sequence of transactions that take place to your action, does it then fall over half way through, wasting time?).
  • Does it make sense to do the check in the UI first? Does it help with your application? If it provides a nicer user experience? (i.e. I have seen cases where you step through several steps in a wizard, it starts, then falls over, when it had all the info it needed to fall over after step 1).
  • Is concurrency an issue? Is it possible that the record may be removed/edited or whatever before your commit takes place (as in the classic File.Exists boo-boo).

In my opinion:

I would do both. If I can fail fast and provide a better user experience, great. Any expected SQL (or any other) exceptions should be getting caught and fed back appropriately anyway.

I know there is a concensus that exceptions should not be used for other than exceptional circumstances, but remember, we are crossing application boundaries here, expect nothing. Like I said, this is like the File.Exists, there is no point, it can be deleted before you access it anyway.

Solution 3:

I think you are right, exceptions should only be used to handle unexpected outcomes, here you are using an exception to deal with an possibly expected outcome, you should deal with this case explicitly but still catch the exception to show a possible error.

Unless this is the way this case is handled all throughout the code, I would side with you. The performance issue should only be brought up if it is actually an issue, i.e. it would depend of the size of those tables and the number of times this function is used.

Solution 4:

Nice question. However, I find the answers ... scary! An exception is a kind of GOTO. I don't like using exceptions in this way because that leads to spaghetti code. Simple as that.

Solution 5:

There's nothing wrong with what you are doing. Exceptions aren't nessesarily "exceptional". They exist to allow calling objects to have fine grained error handling depending on their needs.

Post a Comment for "Catching Exceptions As Expected Program Execution Flow Control?"