Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have to come up with error code and category in order to create <code>error_code</code> object. Here is an example, assuming that error is due to another host refusing connection:</p> <pre><code>error_code ec (errc::connection_refused, system_category()); return ec; </code></pre> <p>You can also pass <code>errno</code> value as error code when using system category. For example:</p> <pre><code>#include &lt;fstream&gt; #include &lt;cerrno&gt; #include &lt;boost/system/system_error.hpp&gt; void foo () { ifstream file ("test.txt"); if (!file.is_open ()) { int err_code = errno; boost::system::error_code ec (err_code , boost::system::system_category ()); throw boost::system::system_error (ec, "cannot open file"); } } </code></pre> <p>Unfortunately, this library is poorly <a href="http://www.boost.org/doc/libs/release/libs/system/doc/index.html" rel="nofollow noreferrer">documented</a>, so I can recommend you to look into <a href="http://www.boost.org/doc/libs/release/boost/system/" rel="nofollow noreferrer">header files</a> to figure things out. The code is fairly simple and straight forward there.</p> <p>Just in case your compiler supports C++11 and you are willing to use it, this functionality made it into standard. As far as I know gcc 4.6.1 has it already. Here is a simple example:</p> <pre><code>#include &lt;cerrno&gt; #include &lt;system_error&gt; std::error_code SystemError::getLastError () { int err_code = errno; return std::error_code (err_code, std::system_category ()); } void foo () { throw std::system_error (getLastError (), "something went wrong"); } </code></pre> <p>Generally, libraries pass <code>error_code</code> object around if there is no need to throw and use <code>system_error</code> to throw an exception describing system failures. Another reason to use <code>error_code</code> without exceptions is when you need to signal the error across different threads. But C++11 has <a href="https://stackoverflow.com/questions/233127/how-can-i-propagate-exceptions-between-threads">a solution for propagating exceptions across threads</a>.</p> <p>Hope it helps!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload