Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8453193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:42:21+00:00 2026-06-10T11:42:21+00:00

I read a thoughtful series of blog posts about the new <system_error> header in

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:42:22+00:00Added an answer on June 10, 2026 at 11:42 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read a long time ago about a rule that using an object we
I read about __packed__ from here and, I understood that when __packed__ is used
Read in some blog that GC in Android happens on main(UI) thread, this may
read many posts about the difference between the 3 idioms. But got more confused,
Read about Server push here . I want to push data to client from
Read about the issue in this stackoverflow question . Still have the same issue
I read an article about a regular expression to detect base64 but when I
read a lot about DataAdapter, DataTable ,.. to reach to this code, in the
Read this question today about safe and unsafe code I then read about it
Read some texts about locking in PHP. They all, mainly, direct to http://php.net/manual/en/function.flock.php .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.