In C++ what are the benefits of using exceptions and try / catch instead of just returning an error code?

后端 未结 13 844
遥遥无期
遥遥无期 2020-12-04 19:58

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 retur

相关标签:
13条回答
  • 2020-12-04 20:36

    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;
    }
    
    0 讨论(0)
  • 2020-12-04 20:37

    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.

    0 讨论(0)
  • 2020-12-04 20:39
    • return an error code when an error condition is expected in some cases
    • throw an exception when an error condition is not expected in any cases

    in the former case the caller of the function must check the error code for the expected failure; in the latter case the exception can be handled by any caller up the stack (or the default handler) as is appropriate

    0 讨论(0)
  • 2020-12-04 20:41

    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.

    0 讨论(0)
  • 2020-12-04 20:41

    Google's C++ Style Guide has a great, thorough analysis of the pros and cons of exception use in C++ code. It also indicates some of the larger questions you should be asking; i.e. do I intend to distribute my code to others (who may have difficulty integrating with an exception-enabled code base)?

    0 讨论(0)
  • 2020-12-04 20:42

    As @Martin pointed out throwing exceptions forces the programmer to handle the error. For example, not checking return codes is one of the biggest sources of security holes in C programs. Exceptions make sure that you handle the error (hopefully) and provide some kind of recover path for your program. And if you choose to ignore an exception rather than introduce a security hole your program crashes.

    0 讨论(0)
提交回复
热议问题