In C++ what are the benefits of using exceptions a

2020-01-30 05:39发布

I've programmed C and C++ for a long time and so far I've never used exceptions and try / catch. What are the benefits of using that instead of just having functions return error codes?

13条回答
兄弟一词,经得起流年.
2楼-- · 2020-01-30 06:18

The advantage of exceptions are two fold:

  • They can't be ignored. You must deal with them at some level, or they will terminate your program. With error code, you must explicitly check for them, or they are lost.

  • They can be ignored. If an error can't be dealt with at one level, it will automatically bubble up to the next level, where it can be. Error codes must be explicitly passed up until they reach the level where it can be dealt with.

查看更多
smile是对你的礼貌
3楼-- · 2020-01-30 06:21

Aside from the other things that were mentioned, you can't return an error code from a constructor. Destructors either, but you should avoid throwing an exception from a destructor too.

查看更多
我命由我不由天
4楼-- · 2020-01-30 06:24

The fact that you have to acknowledge exceptions is correct but this can also be implemented using error structs. You could create a base error class that checks in its dtor whether a certain method ( e.g. IsOk ) has been called. If not, you could log something and then exit, or throw an exception, or raise an assert, etc...

Just calling the IsOk on the error object without reacting to it, would then be the equivalent of writing catch( ... ) {} Both statement would display the same lack of programmer good will.

The transport of the error code up to the correct level is a greater concern. You would basically have to make almost all methods return an error code for the sole reason of propagation. But then again, a function or method should always be annotated with the exceptions it can generate. So basically you have to same problem, without an interface to support it.

查看更多
祖国的老花朵
5楼-- · 2020-01-30 06:25

Sometimes you really have to use an exception in order to flag an exceptional case. For example, if something goes wrong in a constructor and you find it makes sense to notify the caller about this then you have no choice but to throw an exception.

Another example: Sometimes there is no value your function can return to denote an error; any value the function may return denotes success.

int divide(int a, int b)
{
    if( b == 0 )
        // then what?  no integer can be used for an error flag!
    else
        return a / b;
}
查看更多
我只想做你的唯一
6楼-- · 2020-01-30 06:27

Possibly an obvious point - a developer can ignore (or not be aware of) your return status and go on blissfully unaware that something failed.

An exception needs to be acknowledged in some way - it can't be silently ignored without actively putting something in place to do so.

查看更多
闹够了就滚
7楼-- · 2020-01-30 06:27

When I used to teach C++, our standard explanation was that they allowed you to avoid tangling sunny-day and rainy-day scenarios. In other words, you could write a function as if everything would work ok, and catch the exception in the end.

Without exceptions, you would have to get a return value from each call and ensure that it is still legitimate.

A related benefit, of course, is that you don't "waste" your return value on exceptions (and thus allow methods that should be void to be void), and can also return errors from constructors and destructors.

查看更多
登录 后发表回答