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

📄 exceptionhandling.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 handling)机制。 <br>


<br>


在之前还没有学习例外处理之前,如果要撰写一个除法程式,并由使用者输入除数与被除数,由于被除数除以0没有意义,所以之前可能必须像以下这样避免错误: <br>


<div style="margin-left: 40px; font-family: Courier New,Courier,monospace;"><span style="font-weight: bold;">if(b != 0) &nbsp;{</span><br style="font-weight: bold;">


<span style="font-weight: bold;">&nbsp;&nbsp;&nbsp; cout &lt;&lt; "a/b = " </span><br style="font-weight: bold;">


<span style="font-weight: bold;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;&lt; double(a / b) </span><br style="font-weight: bold;">


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


<span style="font-weight: bold;">}</span><br style="font-weight: bold;">


<span style="font-weight: bold;">else {</span><br style="font-weight: bold;">


<span style="font-weight: bold;">&nbsp;&nbsp; cout &lt;&lt; "除数不能为0" </span><br style="font-weight: bold;">


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


<span style="font-weight: bold;">}</span><br>


</div>




<br>




这样的方式过去在一些程式语言中会很常被利用,利用判断式来避免错误的发生,然而这样的方式会让错误处理与程式的处理逻辑混在一起,使得程式难以阅读,在C++中可以利用例外处理机制,将程式逻辑与错误处理适当的分开撰写,增加程式的可读性与安全性。 <br>


<br>


C++中的例外处理使用三个关键字来进行:try、catch、throw,其语法架构如下: <br>


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


<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp; // 程式 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">


<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp; throw Type </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">


<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">} catch(Type 1) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">


<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp; // 错误处理 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">


<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">} catch(Type 2) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">


<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp; // 错误处理 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">


<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>


</div>


<br>


在程式中,当检查到错误发生时,使用throw丢出一个数值,这个数值可能是一个整数、浮点数、字串或是物件,丢出数值之后,程式逻辑会离开丢出点,然后
开始比对catch中设定的资料型态,如果找到对应的型态,就执行该区块中的程式码,执行完后就离开整个try...catch,这有些类似switch
语法,执行完对应的case之后设定break来离开switch区块。 <br>


<br>


先以一个简单的程式作示范: <br>


<br>




<pre>#include &lt;iostream&gt; <br>using namespace std; <br><br>int main() { <br>    int a = 0;<br>    int b = 0; <br><br>    cout &lt;&lt; "请输入被除数: "; <br>    cin &gt;&gt; a; <br>    cout &lt;&lt; "请输入除数: "; <br>    cin &gt;&gt; b;<br><br>    try { <br>        if(b == 0) <br>            throw 0; <br>        cout &lt;&lt; "a / b = " <br>             &lt;&lt; static_cast&lt;double&gt;(a) / b <br>             &lt;&lt; endl; <br>    } <br>    catch(int err) { <br>        cout &lt;&lt; "除数为: " &lt;&lt; err &lt;&lt; endl; <br>        cout &lt;&lt; "结果无限大" &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);">请输入被除数: 10 <br>


请输入除数: 0 <br>


除数为: 0 <br>


结果无限大</span></small><span style="color: rgb(255, 255, 255);"><br>




      </span></td>




    </tr>




  
  
  
  
  </tbody>
</table>




<br>


当使用者输入除数为0时,程式会检查出来,此时丢出一个整数错误代码,程式中catch有一个捕捉整数的区块,当捕捉到错误代码时,就会执行catch区块中的程式码。 <br>


<br>


发生错误时,执行哪一段catch区块是由您所丢出的资料型态而定,丢出整数时就由设定catch整数的区块捕捉,丢出浮点数时就用设定catch浮点数的区块捕捉,您也可以直接丢出一个代表错误讯息的字串,以说明错误的原因,例如: <br>


<br>




<pre>#include &lt;iostream&gt; <br>using namespace std;<br><br>int main() { <br>    int a = 0;<br>    int b = 0;<br><br>    cout &lt;&lt; "请输入被除数: "; <br>    cin &gt;&gt; a; <br>    cout &lt;&lt; "请输入除数: "; <br>    cin &gt;&gt; b;<br><br>    try { <br>        if(b == 0) <br>            throw "发生除零的错误"; <br>        cout &lt;&lt; "a / b = " <br>             &lt;&lt; static_cast&lt;double&gt;(a) / b <br>             &lt;&lt; endl; <br>    } <br>    catch(int err) { <br>        cout &lt;&lt; "除数为: " &lt;&lt; err &lt;&lt; endl; <br>        cout &lt;&lt; "结果无限大" &lt;&lt; endl; <br>    } <br>    catch(const char* str) { <br>        cerr &lt;&lt; "错误: " &lt;&lt; str &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);">请输入被除数: 9 <br>


请输入除数: 0 <br>


错误: 发生除零的错误</span></small><span style="color: rgb(255, 255, 255);"><br>




      </span></td>




    </tr>




  
  
  
  
  </tbody>
</table>




<br>


如果打算在catch中处理完例外之后,再度将例外丢出,则再使用throw即可,例如:<br>

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

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp; // 处理例外 </span><br style="font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; throw; // 再度丢出</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>

</div>



<br>


如果您丢出例外而catch中并没有相对捕捉该例外的区块,则程式会呼叫标准函式库中的terminate()函式,而预设terminate()会呼叫abort()函式来终止程式。 <br>

<br>


如果您想捕捉所有型态的例外,您可以使用catch(...),通常这会放在所有catch之后,以捕捉所有尚未考虑到的例外状况:<br>


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


<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp; // 处理所有的例外 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">


<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>


</div>


<br>


事实上,C++的例外处理鼓励您自订例外类别阶层体系,将这些类别可以当作catch的资料型态设定条件,以应付各种不同的错误处理。 <br>


<br>




</body>
</html>

⌨️ 快捷键说明

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