Re
” what exceptions can “try catch(...)” catch in c++?
It can catch any exception produced by a C++ throw
.
Note by the way that the ellipsis ...
needs to be three periods, not the Unicode …
ellipsis character, otherwise the compiler will choke on it!
Re the code
buf = nullptr;
try{
memcpy(p, buf, l);
}
catch (...){
ofstream f("memcpy.error");
f << "memcpy.error";
}
The call of memcpy
here has formal undefined behavior.
However, a particular C++ implementation can define any behavior, including that which is formally UB, and your compiler Visual C++ may do that, possible with special options. However, Microsoft's documentation of memcpy
does not mention such more well-defined behavior.
On the third hand, undefined behavior includes that you might get some behavior that you erroneously expected, such as the execution going into the catch
clause.
In the 1990's, Microsoft's Visual C++ would catch Windows SEH exceptions (a lower level kind of exception) in catch(...)
, by default. For example, you can get an SEH exception by dereferencing a nullpointer, which might happen in your memcpy
call. Happily the compiler no longer has catching of such exceptions as default, but you can specify that behavior by using the _set_se_translator
function.