您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“C++怎么定制類型異?!?,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“C++怎么定制類型異?!卑?
E.14:使用根據(jù)目的設(shè)計(jì)的用戶定制類型異常(非內(nèi)置類型)
Reason(原因)
A user-defined type is unlikely to clash with other people's exceptions.
用戶定義類型不大可能和其他人的異常發(fā)生沖突。
Example(示例)
void my_code()
{
// ...
throw Moonphase_error{};
// ...
}
void your_code()
{
try {
// ...
my_code();
// ...
}
catch(const Bufferpool_exhausted&) {
// ...
}
}
void my_code() // Don't
{
// ...
throw 7; // 7 means "moon in the 4th quarter"
// ...
}
void your_code() // Don't
{
try {
// ...
my_code();
// ...
}
catch(int i) { // i == 7 means "input buffer too small"
// ...
}
}
The standard-library classes derived from exception should be used only as base classes or for exceptions that require only "generic" handling. Like built-in types, their use could clash with other people's use of them.
繼承自exception的標(biāo)準(zhǔn)庫(kù)類應(yīng)該只用于基類或只要求“通?!碧幚淼漠惓?。和內(nèi)置類型相似,你對(duì)它們的使用可能和其他人的使用發(fā)生沖突。
Example, don't(反面示例)
void my_code() // Don't
{
// ...
throw runtime_error{"moon in the 4th quarter"};
// ...
}
void your_code() // Don't
{
try {
// ...
my_code();
// ...
}
catch(const runtime_error&) { // runtime_error means "input buffer too small"
// ...
}
}
Enforcement(實(shí)施建議)
Catch throw and catch of a built-in type. Maybe warn about throw and catch using a standard-library exception type. Obviously, exceptions derived from the std::exception hierarchy are fine.
捕捉針對(duì)內(nèi)置類型的throw和catch。也許可以針對(duì)使用標(biāo)準(zhǔn)庫(kù)異常類型的throw和catch發(fā)出警告。顯然,繼承自std::exception的異常類沒(méi)有問(wèn)題。
到此,相信大家對(duì)“C++怎么定制類型異?!庇辛烁畹牧私?,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。