<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          C++核心準(zhǔn)則E.2:通過拋出異常來表明函數(shù)無法執(zhí)行指定的任務(wù)

          E.2: Throw an exception to signal that a function can't perform its assigned task

          E.2:通過拋出異常來表明函數(shù)無法執(zhí)行指定的任務(wù)


          Reason(原因)

          To make error handling systematic, robust, and non-repetitive.

          為了使用錯(cuò)誤處理系統(tǒng)化,健壯和不繁瑣。


          Example(示例)

          struct Foo {
          vector v;
          File_handle f;
          string s;
          };

          void use()
          {
          Foo bar {{Thing{1}, Thing{2}, Thing{monkey}}, {"my_file", "r"}, "Here we go!"};
          // ...
          }

          Here,?vector?and?strings constructors may not be able to allocate sufficient memory for their elements,?vectors constructor may not be able copy the?Things in its initializer list, and?File_handle?may not be able to open the required file. In each case, they throw an exception for?use()'s caller to handle. If?use()?could handle the failure to construct?bar?it can take control using?try/catch. In either case,?Foo's constructor correctly destroys constructed members before passing control to whatever tried to create a?Foo. Note that there is no return value that could contain an error code.

          這里vector和string的構(gòu)造函數(shù)可能無法為它們的元素分配足夠的內(nèi)存,vector構(gòu)造函數(shù)可能無法復(fù)制初始化列表中的內(nèi)容,F(xiàn)ile_handle有可能無法打開需要的文件。每種情況中,它們都會(huì)向調(diào)用者拋出異常以便處理。如果use()可以處理構(gòu)造bar對(duì)象時(shí)的錯(cuò)誤,它們可以通過try/catry獲得控制。其他情況下,F(xiàn)oo的構(gòu)造函數(shù)可以在將控制權(quán)交給試圖構(gòu)建Foo的代碼之前正確地銷毀已經(jīng)構(gòu)造完成的成員。注意,代碼中沒有可以容納錯(cuò)誤碼的返回值。

          The?File_handle?constructor might be defined like this:

          File_handle的構(gòu)造函數(shù)可能被定義成下面的樣子:

          File_handle::File_handle(const string& name, const string& mode)
          : f{fopen(name.c_str(), mode.c_str())}
          {
          if (!f)
          throw runtime_error{"File_handle: could not open " + name + " as " + mode};
          }
          Note(注意)

          It is often said that exceptions are meant to signal exceptional events and failures. However, that's a bit circular because "what is exceptional?" Examples:

          一般情況下會(huì)認(rèn)為異常意味著重大的例外事件和錯(cuò)誤。然而,這個(gè)問題多少有點(diǎn)繞,到底什么是例外?例如:

          • A precondition that cannot be met

          • 一個(gè)前提條件沒有滿足

          • A constructor that cannot construct an object (failure to establish its class's?invariant)

          • 構(gòu)造函數(shù)無法構(gòu)造對(duì)象(無法建立類的不變式)

          • An out-of-range error (e.g.,?v[v.size()] = 7)

          • 越界錯(cuò)誤(例如 v[v.size()]=7)

          • Inability to acquire a resource (e.g., the network is down)

          • 無法獲取資源(例如:網(wǎng)絡(luò)斷)

          In contrast, termination of an ordinary loop is not exceptional. Unless the loop was meant to be infinite, termination is normal and expected.

          相反,結(jié)束一個(gè)通常的循環(huán)不屬于異常。只要它不是無限循環(huán),中止就是正常和期待的。


          Note(注意)

          Don't use a?throw?as simply an alternative way of returning a value from a function.

          不要使用將拋出異常作為從函數(shù)中返回結(jié)果的另一種方式使用。


          Exception(例外)

          Some systems, such as hard-real-time systems require a guarantee that an action is taken in a (typically short) constant maximum time known before execution starts. Such systems can use exceptions only if there is tool support for accurately predicting the maximum time to recover from a?throw.

          有些系統(tǒng),例如硬實(shí)時(shí)系統(tǒng)要求保證一個(gè)動(dòng)作在開始執(zhí)行之前就能確定其執(zhí)行時(shí)間小于某個(gè)固定值(通常很?。?。這樣的系統(tǒng)只有在存在某種可以準(zhǔn)確預(yù)測(cè)系統(tǒng)從拋出異常過程中恢復(fù)的最大時(shí)間的工具時(shí)才可以使用異常。


          See also:?RAII

          參見:

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#e6-use-raii-to-prevent-leaks

          See also:?discussion

          參見:

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#discussion-usage-of-noexcept


          Note(注意)

          Before deciding that you cannot afford or don't like exception-based error handling, have a look at the?alternatives; they have their own complexities and problems. Also, as far as possible, measure before making claims about efficiency.

          在決定你無法承擔(dān)或者不喜歡基于例外的錯(cuò)誤處理之前,考慮以下其他選項(xiàng);它們包含自己的復(fù)雜性和問題。同時(shí),只要可能的話,在抱怨效率問題之前進(jìn)行測(cè)量。


          原文鏈接https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#e2-throw-an-exception-to-signal-that-a-function-cant-perform-its-assigned-task

          新書介紹

          以下是本人3月份出版的新書,拜托多多關(guān)注!


          本書利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過可執(zhí)行的示例對(duì)23 個(gè)設(shè)計(jì)模式逐個(gè)進(jìn)行說明。這樣一方面可以使讀者了解真實(shí)的軟件開發(fā)工作中每個(gè)設(shè)計(jì)模式的運(yùn)用場(chǎng)景和想要解決的問題;另一方面通過對(duì)這些問題的解決過程進(jìn)行說明,讓讀者明白在編寫代碼時(shí)如何判斷使用設(shè)計(jì)模式的利弊,并合理運(yùn)用設(shè)計(jì)模式。

          對(duì)設(shè)計(jì)模式感興趣而且希望隨學(xué)隨用的讀者通過本書可以快速跨越從理解到運(yùn)用的門檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書中的示例作為設(shè)計(jì)和開發(fā)的參考;使用Python 語言進(jìn)行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。




          覺得本文有幫助?請(qǐng)分享給更多人。

          關(guān)注微信公眾號(hào)【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!

          面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>



          瀏覽 46
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  无码在校大学生开心内射 | 国产精品操逼片 | 日韩无码中文字幕 | 国产高清色 | 中文无码熟妇人妻 |