⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 standardexception.html

📁 关于 C++ 的历史无须我来介绍了
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>



  
  
  
  <link rel="stylesheet" href="css/stdlayout.css" type="text/css">



  
  
  
  <link rel="stylesheet" href="css/print.css" type="text/css">



  
  
  
  <meta content="text/html; charset=gb2312" http-equiv="content-type">



  
  
  
  <title>标准例外类别</title>
</head>


<body>



<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>



<h1><a href="CppGossip.html">C++
Gossip:&nbsp;标准例外类别</a></h1>



C++提供了标准的例外处理类别,所有的标准例外处理类别都衍生自exception类别,使用它必须包括exception标头档,而
exception类别衍生出来的logic_error类别与runtime_error类别在使用时,则必须包括stdexcept标头档,以下列出
exception与其衍生类别的继承架构: <br>

<div style="margin-left: 40px; font-family: Courier New,Courier,monospace;"><span style="font-weight: bold;">exception </span><br style="font-weight: bold;">

<span style="font-weight: bold;">&nbsp;&nbsp;&nbsp; bad_alloc </span><br style="font-weight: bold;">

<span style="font-weight: bold;">&nbsp;&nbsp;&nbsp; bad_exception </span><br style="font-weight: bold;">

<span style="font-weight: bold;">&nbsp;&nbsp;&nbsp; ios:base:failure </span><br style="font-weight: bold;">

<span style="font-weight: bold;">&nbsp;&nbsp;&nbsp; bad_cast </span><br style="font-weight: bold;">

<span style="font-weight: bold;">&nbsp;&nbsp;&nbsp; bad_typedid </span><br style="font-weight: bold;">

<span style="font-weight: bold;">&nbsp;&nbsp;&nbsp; logic_error </span><br style="font-weight: bold;">

<span style="font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lehgth_error </span><br style="font-weight: bold;">

<span style="font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; domain_error </span><br style="font-weight: bold;">

<span style="font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invalid_argument </span><br style="font-weight: bold;">

<span style="font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; out_of_range </span><br style="font-weight: bold;">

<span style="font-weight: bold;">&nbsp;&nbsp;&nbsp; runtime_error </span><br style="font-weight: bold;">

<span style="font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; overflow_error </span><br style="font-weight: bold;">

<span style="font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; range_error </span><br style="font-weight: bold;">

<span style="font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; underflow_error</span><br>

</div>



<br>

当错误发生而丢出例外物件时,您可以使用what()方法来取得错误讯息,这个错误讯息视您所采用的编译器而有所不同,下面这个程式故意存取超出字串范围的资料,以引发out_of_range例外并显示讯息: <br>

<br>



<pre>#include &lt;iostream&gt; <br>#include &lt;string&gt; <br>#include &lt;stdexcept&gt; <br>using namespace std; <br><br>int main() { <br>    try { <br>        string str("caterpillar"); <br>        cout &lt;&lt; "str.substr(20, 2) = " <br>             &lt;&lt; str.substr(20, 2) <br>             &lt;&lt; endl; <br>    } <br>    catch(out_of_range e) { <br>        cout &lt;&lt; "错误: " <br>             &lt;&lt; e.what() <br>             &lt;&lt; endl; <br>    } <br>    <br>    return 0;<br>}</pre>





<span class="postbody"><br>

执行结果:</span><br>



<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">



  <tbody>



    <tr>



      <td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">错误: basic_string::substr</span></small><span style="color: rgb(255, 255, 255);"><br>



      </span></td>



    </tr>



  
  
  
  </tbody>
</table>



<br>

下面这个程式则是利用回圈不断配置记忆体,直到记忆体不足而发生bad_alloc例外为止,您可以观察到例外的捕捉与讯息:<br>

<br>



<pre>#include &lt;iostream&gt; <br>#include &lt;stdexcept&gt; <br>using namespace std; <br>                                                                                      <br>int main() { <br>    double *ptr; <br>    <br>    do { <br>        try { <br>            ptr = new double[1000000]; <br>        } <br>        catch(bad_alloc err) { <br>            cout &lt;&lt; "配置失败" <br>                 &lt;&lt; err.what() <br>                 &lt;&lt; endl; <br>            return 1; <br>        } <br>        cout &lt;&lt; "配置OK\n"; <br>    } while(ptr); <br>    <br>    return 0;<br>}</pre>





<span class="postbody"><br>

执行结果:</span><br>



<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">



  <tbody>



    <tr>



      <td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">....<br>

配置OK <br>

配置OK <br>

配置OK <br>

配置OK <br>

配置OK <br>

配置OK <br>

配置OK <br>

配置失败St9bad_alloc</span></small><span style="color: rgb(255, 255, 255);"><br>



      </span></td>



    </tr>



  
  
  
  </tbody>
</table>


<br>



</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -