溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C++怎么使用異常發(fā)信號(hào)

發(fā)布時(shí)間:2021-11-25 15:47:35 來(lái)源:億速云 閱讀:182 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要講解了“C++怎么使用異常發(fā)信號(hào)”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“C++怎么使用異常發(fā)信號(hào)”吧!

如果必要任務(wù)失敗了,使用異常發(fā)信號(hào)

Reason(原因)

It should not be possible to ignore an error because that could leave the system or a computation in an undefined (or unexpected) state. This is a major source of errors.

錯(cuò)誤可能會(huì)讓系統(tǒng)或計(jì)算處于沒(méi)有定義(或者意外)的狀態(tài),應(yīng)該杜絕錯(cuò)誤被忽略的可能性。忽略對(duì)錯(cuò)誤的處理是大部分問(wèn)題的主要原因。

Example(示例)

int printf(const char* ...);    // bad: return negative number if output fails
template <class F, class ...Args>// good: throw system_error if unable to start the new threadexplicit thread(F&& f, Args&&... args);
Note(注意)

What is an error?

什么是錯(cuò)誤?

錯(cuò)誤意味著功能無(wú)法達(dá)成它對(duì)外宣稱的目的(包括建立事后條件)。忽略錯(cuò)誤的調(diào)用代碼可能造成錯(cuò)誤的結(jié)果或者沒(méi)有定義的系統(tǒng)狀態(tài)。例如,無(wú)法連接到遠(yuǎn)程服務(wù)器不是它自己造成的錯(cuò)誤:服務(wù)器可能因?yàn)楦鞣N原因拒絕連接,因此自然的做法就是返回一個(gè)結(jié)果讓調(diào)用者確認(rèn)。如果無(wú)法連接被認(rèn)為是錯(cuò)誤,這個(gè)失敗應(yīng)該拋出異常。

Exception(異常)

Many traditional interface functions (e.g., UNIX signal handlers) use error codes (e.g., errno) to report what are really status codes, rather than errors. You don't have a good alternative to using such, so calling these does not violate the rule.

很多傳統(tǒng)的函數(shù)(例如UNIX信號(hào)量處理函數(shù))使用錯(cuò)誤碼(例如errno)報(bào)告實(shí)際的狀態(tài)。由于沒(méi)有好的選項(xiàng)來(lái)代替它們,因此調(diào)用這些函數(shù)不算違反規(guī)則。

Alternative(可選項(xiàng))

If you can't use exceptions (e.g., because your code is full of old-style raw-pointer use or because there are hard-real-time constraints), consider using a style that returns a pair of values:

如果不能使用異常(例如,因?yàn)榇a到處都是舊風(fēng)格的原始指針,或者存在硬實(shí)時(shí)約束),考慮使用返回結(jié)果對(duì)的風(fēng)格。

int val;int error_code;tie(val, error_code) = do_something();if (error_code) {    // ... handle the error or exit ...}
// ... use val ...

譯者注:tie是C++引入的新特性,用于解包同樣是C++11引入的tuple數(shù)據(jù)。

This style unfortunately leads to uninitialized variables. Since C++17 the "structured bindings" feature can be used to initialize variables directly from the return value:

這個(gè)方式有一個(gè)缺點(diǎn)就是會(huì)導(dǎo)致未初始化變量。使用C++17之后導(dǎo)入的結(jié)構(gòu)化綁定功能可以通過(guò)返回值直接初始化變量。

auto [val, error_code] = do_something();if (error_code) {    // ... handle the error or exit ...}// ... use val ...

Note(注意)

We don't consider "performance" a valid reason not to use exceptions.

我們不認(rèn)為“性能”是不使用異常的合理原因。

  • Often, explicit error checking and handling consume as much time and space as exception handling.

    通常,清楚的錯(cuò)誤檢查和處理消耗的時(shí)間和空間和異常處理相同(譯者注:換個(gè)說(shuō)法,目前很多代碼中所謂高效率錯(cuò)誤處理實(shí)際上是偷工減料)

  • Often, cleaner code yields better performance with exceptions (simplifying the tracing of paths through the program and their optimization).

    通常,整潔的代碼使用異常會(huì)帶來(lái)更好的性能(簡(jiǎn)化跟蹤程序執(zhí)行路徑和優(yōu)化)

  • A good rule for performance critical code is to move checking outside the critical part of the code (checking).

       對(duì)于性能敏感的代碼來(lái)說(shuō),將檢查部分移到代碼的關(guān)鍵部分之外是一個(gè)好方法。

  • In the longer term, more regular code gets better optimized.

    長(zhǎng)期來(lái)說(shuō),有規(guī)律的代碼可以被更好地優(yōu)化。

  • Always carefully measure before making performance claims.

    在宣稱性能問(wèn)題之前一定要仔細(xì)地考量。(譯者注:例如真的會(huì)有性能問(wèn)題么?真的那么重要么?)

Enforcement(實(shí)施建議)

  • (Not enforceable) This is a philosophical guideline that is infeasible to check directly.

    (非強(qiáng)制)這是一條事關(guān)編程哲學(xué)的準(zhǔn)則,無(wú)法直接檢查。

  • Look for errno.

    選擇errno。

感謝各位的閱讀,以上就是“C++怎么使用異常發(fā)信號(hào)”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)C++怎么使用異常發(fā)信號(hào)這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

c++
AI