原始碼的分享為提取碼為dq0a
// exception classes for various error types
#ifndef myexceptions_
#define myexceptions_
#include
using
namespace std;
// illegal parameter value
class
illegalparametervalue
void
outputmessage()
private
: string message;};
// illegal input data
class
illegalinputdata
void
outputmessage()
private
: string message;};
// illegal index
class
illegalindex
void
outputmessage()
private
: string message;};
// matrix index out of bounds
class
matrixindexoutofbounds
void
outputmessage()
private
: string message;};
// matrix size mismatch
class
matrixsizemismatch
void
outputmessage()
private
: string message;};
// stack is empty
class
stackempty
void
outputmessage()
private
: string message;};
// queue is empty
class
queueempty
void
outputmessage()
private
: string message;};
// hash table is full
class
hashtablefull
void
outputmessage()
private
: string message;};
// edge weight undefined
class
undefinededgeweight
void
outputmessage()
private
: string message;};
// method undefined
class
undefinedmethod
void
outputmessage()
private
: string message;};
#endif
每個異常類僅有乙個該類的構造器和乙個string型別的訊息成員變數,對於簡單的異常丟擲已經足夠了。
要使用這些異常類時只需要在原始檔新增#inclde 「myexceptions.h」
如果在乙個已經搭建好的圖網路程式中,我們的函式需要檢測圖中的某個節點是否存在,可以這樣設計:
void
checkvertex
(int thevertex)
const
}
在異常判斷語句內建立乙個串流輸出物件s,將"no vertex"語句和傳入變數的節點值作為輸入流寫入s物件,然後使用throw關鍵字丟擲myexceptions.h中的異常類,並將輸出流物件s作為字串形式傳入異常類illegalparametervalue,在類illegalparametervalue中通過構造器將該物件賦值給資料成員message然後輸出。 C 定義自己的String類
我們自己寫的string類具有以下函式 string const char s 用c字串s初始化 string int n,char c 用n個字元c初始化 string string string str const string operator string str string 過載下標訪問...
小程式定義並使用類
目錄結構 暴露定義的變數 let config require config.js 引入變數 定義http類 暴露定義的類export index.js import from utils http.js 引入定義的類let http newhttp 例項化類 page 案例列表 getcasety...
PYTHON 自定義異常類的使用
1.定義乙個異常類,該類從exception類中繼承 2.在try中使用raise語句引發異常 3.except捕獲異常,並執行相關命令 看以下的例子 coding utf 8 使用自定義異常類實現指定輸入字串長度 自定義異常類 class somecustomerror exception def...