I am studying C++11 standards. I wanted to understand if error_code and errno related to each other? If yes then how?
If no then in which conditions i should expect errno to be set and in which conditions error_code will be set?
I did a small test program to understand this but still little confused. Please help.
#include <iostream>
#include <system_error>
#include <thread>
#include <cstring>
#include <cerrno>
#include <cstdio>
using namespace std;
int main()
{
try
{
thread().detach();
} catch (const system_error & e) {
cout<<"Error code value - "<<e.code().value()<<" ; Meaning - "<<e.what()<<endl;
cout<<"Error no. - "<<errno<<" ; Meaning - "<<strerror(errno)<<endl;
}
}
Output -
Error code value - 22 ; Meaning - Invalid argument
Error no. - 0 ; Meaning - Success
errno is used by the those functions that document that as a side effect of their encountering an error - those functions are C library or OS functions that never throw exceptions. system_error is a used by the C++ Standard Library for when you're using library facilities documented to throw that exception. Completely separate. Ultimately, read your docs!
Related
I'm new to programming concepts and using my first time working with such software like Visual Studio. So now I'm learning C++ language.
When I trying to deal with error () function in C++ it gives me an error message saying "Unhandled exception at 0x76DE3E28 in ConsoleApplication3.exe: Microsoft C++ exception: std::runtime_error at memory location 0x0116F8CC.". Here is my code sample:
#include "stdafx.h"
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include <algorithm>
#include "std_lib_facilities.h"
using namespace std;
int area(int w , int l)
{
if (w <= 0 || l <= 0) error("There's something went wrong!");
return w / l;
}
int main()
{
int x = 3;
int y = 0;
cout << area(x, y) << endl;
keep_window_open();
}
I've checked it many times and didn't find anything wrong with the code. Is there anything that I did wrong with that code? Please help me with this guys. It's much appreciate!!
This is one of Bjarne Stroustrup's headers. If you looked at where the exception happened, you would see that an exception is being thrown. What you see is expected. You should not make assumptions about code you just get somewhere. Create your own error function and make it do what you are looking for.
How can I check, which value should I pass to std::locale? I wrote simple program:
#include <cstdio>
#include <locale>
#include <stdexcept>
int main()
{
try
{
std::locale("plk_pol");
}
catch(std::runtime_error)
{
printf("Can't load locale.\n");
return 1;
}
return 0;
}
But it gives me error message. That's strange, because std::setlocale(LC_ALL, 'plk_pol'); works. I tried other values, like pl_PL, polish_poland - behaviour is the same. Even en_US.utf8 gives error. The only locale I was able to set is C.
I have Windows 7 with Polish localisation set. The question is: where can I check, which locales are acceptable in my system?
I read a thoughtful series of blog posts about the new <system_error> header in C++11. It says that the header defines an error_code class that represents a specific error value returned by an operation (such as a system call). It says that the header defines a system_error class, which is an exception class (inherits from runtime_exception) and is used to wrap error_codess.
What I want to know is how to actually convert a system error from errno into a system_error so I can throw it. For example, the POSIX open function reports errors by returning -1 and setting errno, so if I want to throw an exception how should I complete the code below?
void x()
{
fd = open("foo", O_RDWR);
if (fd == -1)
{
throw /* need some code here to make a std::system_error from errno */;
}
}
I randomly tried:
errno = ENOENT;
throw std::system_error();
but the resulting exception returns no information when what() is called.
I know I could do throw errno; but I want to do it the right way, using the new <system_error> header.
There is a constructor for system_error that takes a single error_code as its argument, so if I can just convert errno to error_code then the rest should be obvious.
This seems like a really basic thing, so I don't know why I can't find a good tutorial on it.
I am using gcc 4.4.5 on an ARM processor, if that matters.
You are on the right track, just pass the error code and a std::generic_category object to the std::system_error constructor and it should work.
Example:
#include <assert.h>
#include <errno.h>
#include <iostream>
#include <system_error>
int main()
{
try
{
throw std::system_error(EFAULT, std::generic_category());
}
catch (std::system_error& error)
{
std::cout << "Error: " << error.code() << " - " << error.what() << '\n';
assert(error.code() == std::errc::bad_address);
}
}
Output from the above program on my system is
Error: generic:14 - Bad address
To add to the excellent accepted answer, you can enrich the error message with some contextual information in the 3rd argument, e.g. the failing file name:
std::string file_name = "bad_file_name.txt";
fd = open(file_name, O_RDWR);
if (fd < 0) {
throw std::system_error(errno, std::generic_category(), file_name);
}
Then when caught, e.what() will return, for example:
bad_file_name.txt: file not found
i am using a visual studio c++ compiler,& during my study on exception handling,i came across a number of features that can't be supported by visual c++ compiler,
like controlling the exceptions that can be thrown out of a function.
also i was unable to modify the functioning of terminate() using set_terminate() .
is it a specification too for visual c++ to modify terminate()?...& if so,then can anyone explain that why microsoft is creating these specifications in its compilers?...:-x
what do you mean you were unable to modify terminate
have you tried something like this ?
// set_terminate example
#include <iostream>
#include <exception>
#include <cstdlib>
using namespace std;
void myterminate () {
cerr << "terminate handler called\n";
abort(); // forces abnormal termination
}
int main (void) {
set_terminate (myterminate);
throw 0; // unhandled exception: calls terminate handler
return 0;
}
Don't try to run from VS. Compile and exec from command line.
I know we can use
perror()
in C to print errors. I was just wondering if there is a C++ alternative to this, or whether I have to include this (and therefore stdio.h) in my program. I am trying to avoid as many C functions as possible.
You could do something like:
std::cerr << strerror(errno) << std::endl;
That still ends up calling strerror, so you're really just substituting one C function for another. OTOH, it does let you write via streams, instead of mixing C and C++ output, which is generally a good thing. At least AFAIK, C++ doesn't add anything to the library to act as a substitute for strerror (other than generating an std::string, I'm not sure what it would change from strerror anyway).
You could use the boost::system_error::error_code class.
#include <boost/system/system_error.hpp>
#include <cerrno>
#include <iostream>
void
PrintError(
const std::string& message,
int error
)
{
std::cerr << message << ": " <<
boost::system::error_code(
error,
boost::system::get_system_category()
).message()
<< std::endl;
}
int
main()
{
PrintError( "something went wrong!", EINVAL );
return 0;
}
it's a tad verbose, and somewhat overkill if you aren't already using the boost_system library.
With C++11, we have the <system_error> header, so you should be able to use:
std::error_code{errno, std::generic_category()}.message();
Example program:
#include <system_error>
#include <iostream>
int main() {
std::cout << std::error_code{errno, std::generic_category()}.message() << '\n';
}
This prints Success.
See also:
How to convert errno to exception using <system_error>
<system_error> categories and standard/system error codes (regarding whether to use generic_category or system_category)