why … (three points) in catch block is exist?

試著忘記壹切 提交于 2020-01-06 23:47:05

问题


In the try catch statement we can do:

try{}
catch(...){}

As far as I know, ... means any exception.

My question is: Why the C++ standard chose this way (...) instead of just ()? while, for example, in functions if you do not need parameters you just put ():

void foo();

Is it related to variadic templates in any way?


回答1:


catch() would imply strongly that nothing was passed to that particular catch block.

But that is not true,

catch(...){
    throw;
}

actually re-throws the exception caught by ...




回答2:


It has nothing to do with variadic templates, because those came in C++11 whereas catch (...) existed from nearly the beginning (about two decades earlier).

As for why they chose (...) instead of (), you could ask Bjarne Stroustrup, but it hardly seems important. This feature isn't used all that often anyway. In C++, (...) usually means something like "Any number of things of any types" whereas () usually means "Nothing." Depending on your perspective, either one of these might be more preferred for "catch all exceptions."



来源:https://stackoverflow.com/questions/36737602/why-three-points-in-catch-block-is-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!