📄 standardexception.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: 标准例外类别</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;"> bad_alloc </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> bad_exception </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> ios:base:failure </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> bad_cast </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> bad_typedid </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> logic_error </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> lehgth_error </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> domain_error </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> invalid_argument </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> out_of_range </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> runtime_error </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> overflow_error </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> range_error </span><br style="font-weight: bold;">
<span style="font-weight: bold;"> underflow_error</span><br>
</div>
<br>
当错误发生而丢出例外物件时,您可以使用what()方法来取得错误讯息,这个错误讯息视您所采用的编译器而有所不同,下面这个程式故意存取超出字串范围的资料,以引发out_of_range例外并显示讯息: <br>
<br>
<pre>#include <iostream> <br>#include <string> <br>#include <stdexcept> <br>using namespace std; <br><br>int main() { <br> try { <br> string str("caterpillar"); <br> cout << "str.substr(20, 2) = " <br> << str.substr(20, 2) <br> << endl; <br> } <br> catch(out_of_range e) { <br> cout << "错误: " <br> << e.what() <br> << 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 <iostream> <br>#include <stdexcept> <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 << "配置失败" <br> << err.what() <br> << endl; <br> return 1; <br> } <br> cout << "配置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 + -