开发者

what can't be done with if else clause, and can be done with exception handling?

what can't be done with if else clause, and can be done with exception handling ?

In othe开发者_Python百科r words where do we actually need to use exception handling and mere if else won't serve the purpose.

Is exception handling just a glorified way of showing errors ?


In the earliest C++ implementations, exceptions were compiled into equivalent if/else constructs, more or less. And they were slow as molasses, to the point where you can still find programming guides that recommend against exceptions on the grounds that "they are slow".

So it is not a question of "can" or "cannot". It is a question of readability and performance.

Littering every line of your code with error checks makes it harder for a human to follow the non-exceptional (i.e. common) case. And as every good programmer knows, the primary audience for your code is human readers.

As for performance, if/else is slower than a modern exception implementation. Modern implementations incur literally zero overhead except when an exception is actually thrown. If your exceptions truly represent "exceptional" cases, this can be a significant performance difference.


Some languages (for example C++) don't allow return values in certain cases. For example, in C++ constructors don't have return values (even void), so the only way to signal an error in a constructor is to throw an exception.


Look, there isn't anything you can do with exceptions you can't do with hand-coded 8086 assembler. (Turing complete!) Except that hand-coded assembler isn't a very good tool for a 100K lines-of-code project unless you are the best coder in the Milky Way, if then. Experience shows a number of situations where the idiom of exception handling yields the most robust code written by ordinary humans. Sharptooth's example of ctors is good. So are errors that need to propagate up the call stack. Edit: And how many people actually check that malloc didn't fail every single time? Better to throw an OutOfMemoryException.


Exceptions are just what they are named for: Exceptional events or situations which break the current flow.

You should use exceptions to indicate that something nasty and very exceptional has happened. Let's say you have a class which reads your configuration file. Exceptional situations could be:

  • The file cannot be found.
  • The file exists, but cannot be read.
  • The file was deleted in the middle of a read operation.

You could handle all of these with if-else blocks but it is much simpler to do with exceptions.


Of course, you can write code that does not use exceptions. However, if you do, you would have make sure that any function that could issue an error is handled properly.

For me, the biggest advantage of having exceptions is that I can write straight code that simply assumes that all functions succeeds, knowing that upper layers will take care of reporting errors.

Concretely, take the following fictitious function to work with text in an editor:

do-stuff:
    backward-line 10
    x = point
    search-for "FOO"
    return buffer-substring x point

Both "backward-line" and "search-for" could fail. If I would have to handle errors myself, I would have to check them. In addition, I would have to invent a side-channel to report to my caller that an error occurred. As I said, it can be done, but it would be a lot messier.


Consider a following real-life example. You have a complex recursive function and need to get out of all the calls currently in progress on some condition.

void complexFunction()
{
   //do stuff, then
   if( timeToBailOut() ) {
      // what? how would you get out of all instances at once?
   }       
}

You would need to write those if statements very carefully, perhaps introduce a special return value, check for that value at all times, that would complicate you code greatly. You can easily achieve that behavior with exceptions.

if( timeToBailOut() ) {
    throw BailOutException();
}


There is nothing that can be done with exception handling that can't be done without it, however, some situations would be a pain without exception handling.

Example: a() calls b() and b() calls c(). An error might occur in c() that has to be handled in a(). Without exception handling you would have to deal with it by using sentinel return values in b() and c(), and extra checking code in b(). You can imagine how much more code you would need to write if the error has to be passed through more levels from what caused it to what needs to handle it.

This is a short version of my answer to a different question that was asked here earlier and migrated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜