why ... (three points) in catch block is exist?
Asked Answered
F

2

3

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?

Freedman answered 20/4, 2016 at 8:8 Comment(5)
In C++, unlike in C, () means (void) it doesn't mean any argument but rather a parameter list with no argument.Biphenyl
Read a book.Sequel
it is not duplicated of https://mcmap.net/q/21285/-c-catching-all-exceptions. please at least read the questionFreedman
@JoachimPileborg Thanks I will do thatFreedman
It's just an elegant way to say "anything". Maybe it could be better to use the default key-word in a similar way of switches...Amative
T
4

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 ...

Thorwald answered 20/4, 2016 at 8:31 Comment(1)
nice point.. so internally the exception is cached by a hidden variableFreedman
S
8

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."

Selfregulating answered 20/4, 2016 at 8:11 Comment(1)
It's really often used if you want something robust.Amative
T
4

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 ...

Thorwald answered 20/4, 2016 at 8:31 Comment(1)
nice point.. so internally the exception is cached by a hidden variableFreedman

© 2022 - 2024 — McMap. All rights reserved.