Can anyone explain C++ exception specifications to me?

前端 未结 5 1450
野趣味
野趣味 2021-02-07 21:24

Can anyone explain Exception Specifications as used in C++ ?

  • When are they used (I have rarely seen it used in code)
  • What are the pros and cons (benefits/
5条回答
  •  清歌不尽
    2021-02-07 21:56

    The important thing to know is: exception specifications are deprecated in the next revision of C++, except for the no-throw specifier (throw()), which is basically officially saying "don't use them".

    Putting throw() after a function means the function does not throw any exceptions. If it does anyway, the application will be terminated (probably - the unexpected handler is called), so the rest of your application can use that function knowing it will not throw an exception. This is can be handy for writing exception-safe code.

    Example:

    void MyFunction() throw() // does not throw any exceptions
    {
        /* ... */
    {
    

提交回复
热议问题