📄 customexceptionclass.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类别为基底类别:<br>
<ul>
<li>Exception.h</li>
</ul>
<pre>#ifndef EXCEPTION<br>#define EXCEPTION<br><br>class Exception {<br>public:<br> Exception() {<br> }<br> <br> Exception(const char *message) : _message(message) {<br> }<br> <br> virtual const char* message() {<br> return _message;<br> }<br> <br>protected:<br> const char *_message;<br>};<br><br>#endif</pre>
<br>
假设您要定义一个安全的阵列类别,例如 <a href="ConstructorDestructor.html">建构函式、解构函式</a> 中定义的SafeArray类别,您希望在阵列存取超过阵列长度时丢出一个ArrayIndexOutOfBoundsException,您可以如下继承Exception类别并定义:<br>
<ul>
<li>ArrayIndexOutOfBoundsException.h</li>
</ul>
<pre>#include "Exception.h"<br><br>class ArrayIndexOutOfBoundsException : public Exception {<br>public:<br> ArrayIndexOutOfBoundsException(int);<br><br> ArrayIndexOutOfBoundsException(const char *message) {<br> _message = message;<br> }<br><br> virtual const char* message() {<br> return _message;<br> }<br>};</pre>
<br>
<ul>
<li>ArrayIndexOutOfBoundsException.cpp</li>
</ul>
<pre>#include "ArrayIndexOutOfBoundsException.h"<br>#include <string><br>#include <sstream><br>using namespace std;<br><br>ArrayIndexOutOfBoundsException::ArrayIndexOutOfBoundsException(int index) {<br> string str1;<br> stringstream sstr;<br> sstr << index;<br> sstr >> str1;<br> string str2("ArrayIndexOutOfBoundsException:");<br> str2.append(str1);<br> _message = str2.c_str();<br>}</pre>
<br>
stringstream可用于基本型态与string型态的转换,您将基本型态导入stringstream,再将之导至string中,接着重新定义SafeArray.cpp中的get()与set()函式如下:<br>
<ul>
<li>SafeArray.cpp</li>
</ul>
<pre>#include "SafeArray.h"<br>#include "ArrayIndexOutOfBoundsException.h"<br><br>// 动态配置阵列<br>SafeArray::SafeArray(int len) {<br> length = len;<br> _array = new int[length];<br>}<br><br>// 测试是否超出阵列长度<br>bool SafeArray::isSafe(int i) {<br> // <br> if(i >= length || i < 0) {<br> return false;<br> } <br> else {<br> return true;<br> }<br>}<br><br>// 取得阵列元素值<br>int SafeArray::get(int i) {<br> if(isSafe(i)) {<br> return _array[i]; <br> }<br> else {<br> // 存取超过阵列长度,丢出例外 <br> throw ArrayIndexOutOfBoundsException(i); <br> }<br>}<br><br>// 设定阵列元素值<br>void SafeArray::set(int i, int value) {<br> if(isSafe(i)) {<br> _array[i] = value;<br> }<br> else {<br> // 存取超过阵列长度,丢出例外<br> throw ArrayIndexOutOfBoundsException(i); <br> }<br>}<br><br>// 删除动态配置的资源<br>SafeArray::~SafeArray() {<br> delete [] _array;<br>}</pre>
<br>
在阵列存取超过阵列长度时丢出一个ArrayIndexOutOfBoundsException,您可以如下使用try...catch来捕捉例外:<br>
<ul>
<li>main.cpp</li>
</ul>
<pre>#include <iostream> <br>#include "SafeArray.h"<br>#include "Exception.h"<br>#include "ArrayIndexOutOfBoundsException.h"<br>using namespace std; <br><br>int main() {<br> SafeArray safeArray(10);<br> <br> try {<br> // 故意存取超过阵列长度<br> for(int i = 0; i <= safeArray.length; i++) {<br> safeArray.set(i, (i + 1) * 10);<br> }<br> <br> for(int i = 0; i < safeArray.length; i++) {<br> cout << safeArray.get(i) << " ";<br> }<br> <br> cout << endl;<br> }<br> catch(ArrayIndexOutOfBoundsException e) {<br> cout << endl<br> << e.message()<br> << endl;<br> }<br> catch(Exception e) {<br> cout << endl<br> << e.message()<br> << endl;<br> }<br> <br> return 0;<br>}<br></pre>
<br>
<span class="postbody">
执行结果:</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);">ArrayIndexOutOfBoundsException:10</span></small><span style="color: rgb(255, 255, 255);"><br>
</span></td>
</tr>
</tbody>
</table>
<br>
在try...catch的最后一个catch您捕捉了Exception型态的例外,这可以捕捉所有Exception及其子类别的例外,这避免了直接使用catch(...)来一网打尽式的捕捉例外的方式。<br>
<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -