C ++ equivalent to perror?

As far as I know, my two options for handling stream errors are perror and stream exceptions. Both options are undesirable. That's why:

PError

  • std::strerror returns a message defined by the implementation. This message is not always useful for all systems.

  • std::strerror not thread safe

  • The pointer returned by std::strerror may be invalid

  • The C library is technically "deprecated" in C ++. Almost always, the C ++ equivalent is ish. There is no reason why I have to rely on POSIX and C when there is a potential C ++ equivalent

flow exceptions

  • Exceptions are not suitable for each program.

  • Although std::strerror can sometimes provide a useful error message, stream exceptions never provide a useful error message. With f.exceptions(f.failbit) , in order to fail to open the file, the exception std::ios_base::failure selected, and what() is " basic_ios::clear ".

system_error

Replacing std::ios_base::failure with std::system_error gives exactly the same results. If we look at N2769: Detailed Reporting for Input/Output Library Errors (Revision 2) , we can understand why:

When throwing an ios_base::failure exception, ios_base::failure recommended that the implementation provide ec values ​​that identify the specific reason for the failure. [ Note . Errors resulting from the operation are usually reported as system_category errors with the error number of the error number reported by the operating system. Errors arising from the stream library, usually like error_code(ioerrc::stream, iostream_category ) - end note ].

The above wording only provides regulatory incentives for performers to do the right thing and, therefore, relies on market forces and good intentions of performers to get results that are useful to users. Anything stronger (for example, a change of “encouraged” to “should”) will require a lot of additional specification and go beyond the scope that the LWG can really solve for C ++ 0x.

The alternative is to potentially rely on a cumbersome third-party library (I look at you in Boost) or a roll-up game (which you will never want to do if you don’t know what you are doing.) I am looking for a standard C ++ library for this. Whether there is a?

+8
c ++ error-handling
source share
2 answers

I do not agree with you in

perror et alii are members of the C standard library and as such are technically outdated (*).

Well, even on cplusplus.com, if not an official source of documentation, I see for errno:

C ++ 11 extends the basic set of values ​​needed to be defined in this header ... In C ++, errno is always declared as a macro, but in C it can also be implemented as an int object with external connection.

My understanding of these suggestions is that errno is still being considered in C ++ 11!

I even see later

Libraries supporting multithreading must implement errno on each line: each thread has its own local errno. This is a requirement in libraries complying with C11 and C ++ 11 standards.

this means that even if strerror() not thread safe, errno is in any C ++ 11 compatible implementation.

Now you need to use std::strerror in a synchronized function (e.g. protected with a mutex) to build std::string if you want. But in any case, even if every documentation in strerror claims to use a static buffer that can be overwritten (why Posix 1 defines strerror_r), I could not find any warning in perror , which is not a safe stream.

(*) My opinion is that whenever OO and templates can provide a more convenient interface (iostream vs stdio.h or string vs. string.h), elements of the C library are replaced by others in the C ++ library. But when there is nothing to add (cerrno, cmath, csignal, etc.), Functions from the C standard library are simply included in C ++.

+1
source share

POSIX has strerror_r() , and Windows has strerror_s() . Note that there were several incompatible functions called strerror_r() , so use your macros to test the functions.

0
source share

All Articles