what does this declaration mean? exception() throw()

后端 未结 5 1172
暗喜
暗喜 2021-01-12 05:34

std::exception class is defined as follows

exception() throw() { }
virtual ~exception() throw();
virtual const char* what() const throw();

相关标签:
5条回答
  • 2021-01-12 05:43

    It's an "exception specification". throw() means "this function will not throw any exceptions". You can also specify exceptions, so throw(foo) would say this function may throw exceptions of type foo.

    The usefulness of this feature has been debated quite a bit in the C++ community - the general evaluation seems to be that it is not particularly useful. For more details take a look at this Herb Sutter article.

    0 讨论(0)
  • 2021-01-12 05:55

    Without any parameter, it means that the mentioned functions does not throw any exceptions.

    If you specify anything as a parameter, you're saying that the function will throw only exceptions of that type. Notice, however, this is not an enforcement to the compiler. If an exception of some other type happens to be thrown the program will call std::terminate().

    0 讨论(0)
  • 2021-01-12 05:58

    Can throw() take parameters?

    Yes it can be used to declare what parameteres the method is allowed to throw.

    Also the destructor is marked as throw(), destructors should never ever throw exceptions as they may already be executing in the context of a thrown exception.

    0 讨论(0)
  • 2021-01-12 06:02

    It's an exception specification. No arguments means that the function can't throw any exceptions.

    0 讨论(0)
  • 2021-01-12 06:06

    This is called a throw specification. It defines which exceptions (if any) can be thrown from the function.

    These sound great in theory, but there are issues with using them.

    A good discussion about this can be found at this SO question.

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