0

The code

    try{
        memcpy(p, buf, l);
    }
    catch (...){
        ofstream f("memcpy.error");
        f << "memcpy.error";
    }

if p is null,memcpy will throw exception,but catch(...)can't catch it.So,in c++,what do try catch(...) actually do?

Yuan Wen
  • 1,583
  • 3
  • 20
  • 38
  • 2
    `memcpy` does not throw exceptions. – PaulMcKenzie Feb 28 '16 at 04:52
  • 1
    Are you talking about Access Violations? And possibly structured exception handling on Visual Studio? https://msdn.microsoft.com/en-us/library/swezty51.aspx – drescherjm Feb 28 '16 at 04:53
  • Yes,because if p is null,vs will show First-chance exception at 0x5 (msvcr120d.dll) in.exe: 0xC Access violation writing location 0x0. – Yuan Wen Feb 28 '16 at 04:55
  • @YuanWen That is **not** a C++ exception that is a Structured Exception. Please note the differences. http://stackoverflow.com/questions/2782915/what-should-i-know-about-structured-exceptions-seh-in-c – PaulMcKenzie Feb 28 '16 at 04:59
  • 1
    I added Visual Studio to the tags because I believe the question is really about a Microsoft Visual Studio extension. I guess I could have used Visual-c++ instead. – drescherjm Feb 28 '16 at 05:00
  • You mean catch(...) can only handle c++ exception,but not other type exceptions?@PaulMcKenzie – Yuan Wen Feb 28 '16 at 05:00
  • 1
    Possible duplicate of [How can I handle an access violation in Visual Studio C++?](http://stackoverflow.com/questions/14610879/how-can-i-handle-an-access-violation-in-visual-studio-c) – Weak to Enuma Elish Feb 28 '16 at 05:00
  • @YuanWen Ever hear of `throw`? That is what Microsoft does in their C++ runtime when a Structured Exception occurs. An access violation SE knows nothing about C++. It is the Visual Studio C++ runtime that is designed to detect the Access violation and then call `throw`. – PaulMcKenzie Feb 28 '16 at 05:04

3 Answers3

3

Answering in the context of the question being tagged as [visual-studio] and [winapi] by implication.

try/catch blocks catch C++ exceptions, while memcpy with a NULL destination throws a Win32 structured exception.

You can catch those exceptions with __try/__except blocks (Structured Exception Handling).

Alternatively, you can convert them to C++ exceptions catchable in C++ try/catch blocks via _set_se_translator.

dxiv
  • 16,984
  • 2
  • 27
  • 49
2

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.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
1

If p is null, memcpy will throw an exception.

Incorrect. If either pointer is null, it is undefined behavior. Since undefined is "undefined", there isn't much you can do about it when it happens (in a non-implementation dependent way).

Weak to Enuma Elish
  • 4,622
  • 3
  • 24
  • 36