Files
hustoj/web/cppreference-zh/chmhelp/cpp-error-system_error-system_error.html
2025-08-01 12:57:54 +08:00

140 lines
9.9 KiB
HTML

<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs" xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh">
<head><meta http-equiv="x-ua-compatible" content="ie=edge">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>std::system_error::system_error</title>
<link rel="stylesheet" href="ext.css" />
<link rel="stylesheet" href="site_modules.css" />
</head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_error_system_error_system_error skin-cppreference2 action-view cpp-navbar">
<div id="cpp-content-base">
<div id="content"><a id="top"></a>
<h1 id="firstHeading" class="firstHeading"><span style="font-size:0.7em; line-height:130%">std::system_error::</span>system_error</h1>
<div id="bodyContent">
<div id="contentSub"><span class="subpages">&lt; <a href="cpp.html">cpp</a>&lrm; | <a href="cpp-error.html">error</a>&lrm; | <a href="cpp-error-system_error.html">system error</a></span></div>
<div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr" xml:lang="zh">
<table class="t-dcl-begin">
<tbody>
<tr class="t-dcl t-since-cxx11">
<td>
<div><span class="mw-geshi cpp source-cpp">system_error<span class="br0">(</span> <a href="cpp-error-error_code.html"><span class="kw877">std::<span class="me2">error_code</span></span></a> ec <span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td>(1)</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx11">
<td>
<div><span class="mw-geshi cpp source-cpp">system_error<span class="br0">(</span> <a href="cpp-error-error_code.html"><span class="kw877">std::<span class="me2">error_code</span></span></a> ec, <span class="kw4">const</span> <a href="cpp-string-basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy3">&amp;</span> what_arg <span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td>(2)</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx11">
<td>
<div><span class="mw-geshi cpp source-cpp">system_error<span class="br0">(</span> <a href="cpp-error-error_code.html"><span class="kw877">std::<span class="me2">error_code</span></span></a> ec, <span class="kw4">const</span> <span class="kw4">char</span><span class="sy2">*</span> what_arg <span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td>(2)</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx11">
<td>
<div><span class="mw-geshi cpp source-cpp">system_error<span class="br0">(</span> <span class="kw4">int</span> ev, <span class="kw4">const</span> <a href="cpp-error-error_category.html"><span class="kw871">std::<span class="me2">error_category</span></span></a><span class="sy3">&amp;</span> ecat <span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td>(3)</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx11">
<td>
<div><span class="mw-geshi cpp source-cpp">system_error<span class="br0">(</span> <span class="kw4">int</span> ev, <span class="kw4">const</span> <a href="cpp-error-error_category.html"><span class="kw871">std::<span class="me2">error_category</span></span></a><span class="sy3">&amp;</span> ecat,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">const</span> <a href="cpp-string-basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy3">&amp;</span> what_arg <span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td>(4)</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx11">
<td>
<div><span class="mw-geshi cpp source-cpp">system_error<span class="br0">(</span> <span class="kw4">int</span> ev, <span class="kw4">const</span> <a href="cpp-error-error_category.html"><span class="kw871">std::<span class="me2">error_category</span></span></a><span class="sy3">&amp;</span> ecat,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">const</span> <span class="kw4">char</span><span class="sy2">*</span> what_arg <span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td>(4)</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx11">
<td>
<div><span class="mw-geshi cpp source-cpp">system_error<span class="br0">(</span> <span class="kw4">const</span> system_error<span class="sy3">&amp;</span> other <span class="br0">)</span> <span class="kw1">noexcept</span><span class="sy4">;</span></span></div>
</td>
<td>(5)</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span></td>
</tr>
<tr class="t-dcl-sep">
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p>&#26500;&#36896;&#26032;&#30340; system_error &#23545;&#35937;&#12290;</p>
<div class="t-li1"><span class="t-li">1)</span> &#20197; error_code <code>ec</code> &#26500;&#36896;</div>
<div class="t-li1"><span class="t-li">2)</span> &#20197; error_code <code>ec</code> &#21644;&#35299;&#37322;&#23383;&#31526;&#20018; <code>what_arg</code> &#26500;&#36896;&#12290; <span class="t-lc"><a href="cpp-error-system_error-what.html">what()</a></span> &#36820;&#22238;&#30340;&#23383;&#31526;&#20018;&#20445;&#35777;&#21547;&#26377; <code>what_arg</code> &#12290;</div>
<div class="t-li1"><span class="t-li">3)</span> &#20197;&#24213;&#23618;&#38169;&#35823;&#30721; <code>ev</code> &#21644;&#20851;&#32852;&#30340; error_category <code>ecat</code> &#26500;&#36896;&#12290;</div>
<div class="t-li1"><span class="t-li">4)</span> &#20197;&#24213;&#23618;&#38169;&#35823;&#30721; <code>ev</code> &#12289;&#20851;&#32852;&#30340; error_category <code>ecat</code> &#21644;&#35299;&#37322;&#23383;&#31526;&#20018; <code>what_arg</code> &#26500;&#36896;&#12290; <span class="t-lc"><a href="cpp-error-system_error-what.html">what()</a></span> &#36820;&#22238;&#30340;&#23383;&#31526;&#20018;&#20445;&#35777;&#21547;&#26377; <code>what_arg</code> &#65288;&#20551;&#23450;&#23427;&#19981;&#21547;&#20869;&#23884;&#30340;&#31354;&#23383;&#31526; &#65289;&#12290;</div>
<div class="t-li1"><span class="t-li">5)</span> &#22797;&#21046;&#26500;&#36896;&#20989;&#25968;&#12290;&#20197; <code>other</code> &#30340;&#20869;&#23481;&#21021;&#22987;&#21270;&#20869;&#23481;&#12290;&#33509; <code>*this</code> &#19982; <code>other</code> &#22343;&#25317;&#26377;&#21160;&#24577;&#31867;&#22411; <code>std::system_error</code> &#21017; <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="cpp-string-byte-strcmp.html"><span class="kw1146">std::<span class="me2">strcmp</span></span></a><span class="br0">(</span>what<span class="br0">(</span><span class="br0">)</span>, other.<span class="me1">what</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span> <span class="sy1">==</span> <span class="nu0">0</span></span></span> &#12290;</div>
<h3><span class="mw-headline" id=".E5.8F.82.E6.95.B0">&#21442;&#25968;</span></h3>
<table class="t-par-begin">
<tr class="t-par">
<td>ec</td>
<td>-</td>
<td>&#38169;&#35823;&#30721;</td>
</tr>
<tr class="t-par">
<td>ev</td>
<td>-</td>
<td>&#22522;&#30784;&#32534;&#30721;&#30340;&#38169;&#35823;&#30721;</td>
</tr>
<tr class="t-par">
<td>ecat</td>
<td>-</td>
<td>&#38169;&#35823;&#31867;&#21035;</td>
</tr>
<tr class="t-par">
<td>what_arg</td>
<td>-</td>
<td>&#35299;&#37322;&#24615;&#23383;&#31526;&#20018;</td>
</tr>
</table>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">&#31034;&#20363;</span></h3>
<div class="t-example">
<p>&#28436;&#31034;&#22914;&#20309;&#20174; errno &#20540;&#21019;&#24314; system_error &#24322;&#24120;</p>
<div class="t-example-live-link"></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;">
<div class="cpp source-cpp">
<pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="co2">#include &lt;system_error&gt;</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
<span class="kw1">try</span>
<span class="br0">{</span>
<span class="kw1">throw</span> <a href="cpp-error-system_error.html"><span class="kw959">std::<span class="me2">system_error</span></span></a><span class="br0">(</span><a href="cpp-error-errno_macros.html"><span class="kw810">EDOM</span></a>, <a href="cpp-error-generic_category.html"><span class="kw872">std::<span class="me2">generic_category</span></span></a><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span>
<span class="kw1">catch</span> <span class="br0">(</span><span class="kw4">const</span> <a href="cpp-error-system_error.html"><span class="kw959">std::<span class="me2">system_error</span></span></a><span class="sy3">&amp;</span> error<span class="br0">)</span>
<span class="br0">{</span>
<a href="cpp-io-cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">&quot;Error: &quot;</span> <span class="sy1">&lt;&lt;</span> error.<span class="me1">code</span><span class="br0">(</span><span class="br0">)</span>
<span class="sy1">&lt;&lt;</span> <span class="st0">&quot; - &quot;</span> <span class="sy1">&lt;&lt;</span> error.<span class="me1">code</span><span class="br0">(</span><span class="br0">)</span>.<span class="me1">message</span><span class="br0">(</span><span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">&#39;<span class="es1">\n</span>&#39;</span><span class="sy4">;</span>
<span class="br0">}</span>
<span class="br0">}</span></pre></div>
</div>
<p>&#21487;&#33021;&#30340;&#36755;&#20986;&#65306;</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;">
<div class="text source-text">
<pre class="de1">Error: generic:33 - Numerical argument out of domain</pre></div>
</div>
</div>
</div>
<div class="visualClear"></div>
</div>
</div>
</div>
</body>
</html>