0

It's a pretty popular and well known phrase that you should "only catch/throw exceptions which are exceptional". However, how is an "exceptional" exception determined?

For example, a bad password is very routine in logging into a service, so this is not exceptional. Statistics for a web app would probably show something like one bad login attempt for every 5 attempts (from no specific user). Likewise, with attempting to go to a checkout with a basket in an online store, this could be very commmon (especially for new users). However, a file not found could go either way. I usually work along the lines that if a method is missing something to do its work, throw an exception, but then it gets a little confusing here. In some cases, a file not found could be common (e.g. a file share used by many users with no tight controls), compared to a very locked down production environment missing a file, which would be exceptional.

Is this the right way to deduce between whether an exception is exceptional or not? I can easily filter things like no network connection etc as exceptional, but some cases are hard to judge. Is it subjective?

Thanks

GurdeepS
  • 65,107
  • 109
  • 251
  • 387
  • Just because it's common, doesn't mean it should not cause an exception. – nos May 01 '11 at 00:49
  • Is this really a well known phrase? What exactly does it mean? Where did you see someone define it? It seems you are asking to define what this _well known_ phrase means and yet I don't think it is well known. I was not able to find _any_ mention of it in a google search. – Hogan May 01 '11 at 00:52
  • However, if your question really is -- when should my code not handle an exception (that is _throw_). The answer is simple: When something happens that your code can't recover from. _Of course like everything else there are exceptions (pun intended) to this rule_ – Hogan May 01 '11 at 00:54

1 Answers1

1

I think it's pretty subjective, honestly, so I prefer to avoid that method of figuring out when I should use exceptions.

Instead, I prefer to consider three things:

  1. Is it likely that I might want to let the call stack unwind more than one level?
  2. Is there another way? (Return null or an error code, etc.) If so, do I have even the slightest performance concern?
  3. If neither of those lead to a clear decision, which is easier to read by someone who has to maintain the code?

If #1 is true, and I don't have a MAJOR performance concern, I will probably opt to use exceptions because it will speed up my development time not to have to code return codes (and manually code the logic to have them propagate up the call stack if needed). When you use exceptions, call stack unwinding is free of charge for development time.

If #2 is true, and either I'm not going more than one frame (maybe two?) up the call stack or I have a serious performance concern (in a tight loop, for example), then I'll try really hard to find another way that doesn't involve exceptions.

Exceptions are only a tool for programmers in a language which supports them. I don't believe they have to have any intrinsic value as to what is "exceptional" or not. Instead, I say use them when they are the best tool for the job.

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
  • 1
    Other considerations occur in languages such as C++, such as if one chooses to use exceptions, all code must be exception-safe, i.e. must release resource correctly. – Oliver Charlesworth May 01 '11 at 00:59
  • Good way of thinking. What do you mean by "let the call stack unwind more than one level"? – GurdeepS May 01 '11 at 00:59
  • @dotnetdev: If you want to catch the exception a few levels up (i.e., you don't want to catch the exception in the same function where you called the exceptional code). Exceptions will naturally go back up the call stack, discarding function contexts in the reverse order of how they are called. If you want to use a return code, you have to have handling logic in EACH intervening function level, e.g., `if (errorCode != SUCCESS) return errorCode;`, and sometimes that can be a pain to write ten times. – Platinum Azure May 12 '11 at 16:21
  • I see. So you're just letting the exception progate. – GurdeepS May 22 '11 at 18:44