1

In the below code, author points that new operator function call might cause an exception so that this implementation is not exception safe because object state is already changed in the first line.

String &String::operator =( const char *str ) {
    // state is changed
    delete [] s_;                                        

    if( !str ) str = "";

    // exception might occur because of new operator
    s_ = strcpy( new char[ strlen(str)+1 ], str );

    return *this;
}

While reading, I wondered that do C library functions throw exception in C++ ? I know that there is no exception in C but since we are using C++ compiler, there might be exceptions.

So, can we consider c standard lib functions as exception safe function calls ?

Thank you.

Btw, for the record, right way (exception safe) to implement above function is below.

String &String::operator =( const char *str ) {
    if( !str ) str = "";
    char *tmp = strcpy( new char[ strlen(str)+1 ], str );
    delete [] s_;
    s_ = tmp;
    return *this;
}
Validus Oculus
  • 2,756
  • 1
  • 25
  • 34

3 Answers3

5

While reading, I wondered that do C library functions throw exception in C++ ?

Not if you use the functions correctly. How could they? That would completely break backward compatibility.

However, if you use them incorrectly, undefined behaviour occurs. And in that case, a compiler can do anything it wants, including the throwing of exceptions.

For example, take the following program which exhibits undefined behaviour according to the C++ language standard:

#include <iostream>
#include <string.h>

int main()
{
    try
    {
        strcpy(nullptr, nullptr);
    }
    catch (...)
    {
        std::cerr << "exception\n";
    }
}

Compile it with Visual C++ 2013 using Structured Exception Handling as follows:

cl /nologo /Za /EHa /W4 stackoverflow.cpp

Result of the program:

exception
Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
3

Generally speaking, the C++ standard does not modify any of the functions from the C standard, except to decree the identifiers should refer to actual functions rather than macros. Thus, functions from the C standard library cannot throw an exception in any circumstance there the C standard decrees what a function can do.

Loopholes that would allow exceptions include things like undefined behavior or callbacks (e.g. passing a comparator to qsort that throws an exception... although I'm not actually sure if that's allowed by the C++ standard). I don't think it particularly likely that library providers would spend any effort trying to add a feature of throwing exceptions in situations where there is undefined behavior.

0

No - just because you compile with a C++ compiler doesn't mean exceptions will get thrown. C does not support exceptions, so a C program cannot throw any sort of exception.

rep_movsd
  • 6,675
  • 4
  • 30
  • 34
  • So, do you mean that regardless of doing what, I can always consider c functions as exception safe right ? Btw, I am writing C++ applications but sometimes, I am using c std functions. – Validus Oculus Mar 08 '15 at 11:35
  • Exception safe doesn't mean it cannot cause an exceptional condition - C code can still raise signals and processor level exceptions. Or the runtime can call abort() ending your entire program without any warning. You can use SEH under windows and wrap code in catch(...) - Then the OS/compiler translate low level exceptions into C++ language exceptions. This is not a good practice, but sometimes it may be necessary. – rep_movsd Mar 08 '15 at 11:39
  • 1
    If he compiles with a C++ compiler, he has a C++ program, not a C program. A C++ standard library implementation is free to throw an exception from one of the functions in the C subset. I don't know if any implementation does that, but it is certainly possible. – Benjamin Lindley Mar 08 '15 at 11:42
  • 1
    C code can throw exceptions too, I think; e.g. `qsort` will throw an exception if the comparison function you give it does. –  Mar 08 '15 at 11:43
  • @rep_movsd, with all due respect but you are making it complicated. This is generally known as hiding behind the complexity :) Question is simple. Does strcpy throw an exception which I can catch ? Is there anything in standards ? – Validus Oculus Mar 08 '15 at 11:44
  • @Benjamin - gcc and MSVC both will compile a .C file as C code, not as C++, otherwise they would be non-standard. The C standard does not define anything to do with exceptions. You cannot include a C++ header in a C program, nor can you link C++ code from a C program. If you call a function that was compiled in C++ in a DLL, with a throw statement, it will not propagate, it's likely that the program will terminate. MSVC has its own extensions for C based exceptions, meant to catch SEH - the equivalent in Linux are signals https://msdn.microsoft.com/en-us/library/9xtt5hxz(v=vs.71).aspx – rep_movsd Mar 08 '15 at 13:19
  • @Murat - I'm afraid it is complicated :) - If you program C and C++ and often interact with the OS APIs, you need to be aware of C++ exceptions and SEH – rep_movsd Mar 08 '15 at 13:20