久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <bdo id='dW4Mz'></bdo><ul id='dW4Mz'></ul>
<i id='dW4Mz'><tr id='dW4Mz'><dt id='dW4Mz'><q id='dW4Mz'><span id='dW4Mz'><b id='dW4Mz'><form id='dW4Mz'><ins id='dW4Mz'></ins><ul id='dW4Mz'></ul><sub id='dW4Mz'></sub></form><legend id='dW4Mz'></legend><bdo id='dW4Mz'><pre id='dW4Mz'><center id='dW4Mz'></center></pre></bdo></b><th id='dW4Mz'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='dW4Mz'><tfoot id='dW4Mz'></tfoot><dl id='dW4Mz'><fieldset id='dW4Mz'></fieldset></dl></div>

    <small id='dW4Mz'></small><noframes id='dW4Mz'>

      1. <legend id='dW4Mz'><style id='dW4Mz'><dir id='dW4Mz'><q id='dW4Mz'></q></dir></style></legend>
      2. <tfoot id='dW4Mz'></tfoot>

        如何拋出 C++ 異常

        How to throw a C++ exception(如何拋出 C++ 異常)
      3. <legend id='NOkgc'><style id='NOkgc'><dir id='NOkgc'><q id='NOkgc'></q></dir></style></legend>

          <bdo id='NOkgc'></bdo><ul id='NOkgc'></ul>

            <small id='NOkgc'></small><noframes id='NOkgc'>

              <tfoot id='NOkgc'></tfoot>
              <i id='NOkgc'><tr id='NOkgc'><dt id='NOkgc'><q id='NOkgc'><span id='NOkgc'><b id='NOkgc'><form id='NOkgc'><ins id='NOkgc'></ins><ul id='NOkgc'></ul><sub id='NOkgc'></sub></form><legend id='NOkgc'></legend><bdo id='NOkgc'><pre id='NOkgc'><center id='NOkgc'></center></pre></bdo></b><th id='NOkgc'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='NOkgc'><tfoot id='NOkgc'></tfoot><dl id='NOkgc'><fieldset id='NOkgc'></fieldset></dl></div>

                  <tbody id='NOkgc'></tbody>

                • 本文介紹了如何拋出 C++ 異常的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

                  我對(duì)異常處理的理解很差(即,如何為自己的目的自定義 throw、try、catch 語(yǔ)句).

                  I have a very poor understanding of exception handling(i.e., how to customize throw, try, catch statements for my own purposes).

                  例如我定義了一個(gè)函數(shù)如下:int compare(int a, int b){...}

                  For example, I have defined a function as follows: int compare(int a, int b){...}

                  我希望函數(shù)在 a 或 b 為負(fù)數(shù)時(shí)拋出一個(gè)帶有一些消息的異常.

                  I'd like the function to throw an exception with some message when either a or b is negative.

                  我應(yīng)該如何在函數(shù)定義中解決這個(gè)問(wèn)題?

                  How should I approach this in the definition of the function?

                  推薦答案

                  簡(jiǎn)單:

                  #include <stdexcept>
                  
                  int compare( int a, int b ) {
                      if ( a < 0 || b < 0 ) {
                          throw std::invalid_argument( "received negative value" );
                      }
                  }
                  

                  標(biāo)準(zhǔn)庫(kù)附帶了一個(gè)很好的內(nèi)置異常對(duì)象你可以扔.請(qǐng)記住,您應(yīng)該始終按值拋出并按引用捕獲:

                  The Standard Library comes with a nice collection of built-in exception objects you can throw. Keep in mind that you should always throw by value and catch by reference:

                  try {
                      compare( -1, 3 );
                  }
                  catch( const std::invalid_argument& e ) {
                      // do stuff with exception... 
                  }
                  

                  您可以在每次嘗試后使用多個(gè) catch() 語(yǔ)句,因此您可以根據(jù)需要分別處理不同的異常類型.

                  You can have multiple catch() statements after each try, so you can handle different exception types separately if you want.

                  你也可以重新拋出異常:

                  You can also re-throw exceptions:

                  catch( const std::invalid_argument& e ) {
                      // do something
                  
                      // let someone higher up the call stack handle it if they want
                      throw;
                  }
                  

                  并捕獲不分類型的異常:

                  And to catch exceptions regardless of type:

                  catch( ... ) { };
                  

                  這篇關(guān)于如何拋出 C++ 異常的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  In what ways do C++ exceptions slow down code when there are no exceptions thown?(當(dāng)沒(méi)有異常時(shí),C++ 異常會(huì)以何種方式減慢代碼速度?)
                  Why catch an exception as reference-to-const?(為什么要捕獲異常作為對(duì) const 的引用?)
                  When and how should I use exception handling?(我應(yīng)該何時(shí)以及如何使用異常處理?)
                  Scope of exception object in C++(C++中異常對(duì)象的范圍)
                  Catching exceptions from a constructor#39;s initializer list(從構(gòu)造函數(shù)的初始化列表中捕獲異常)
                  Difference between C++03 throw() specifier C++11 noexcept(C++03 throw() 說(shuō)明符 C++11 noexcept 之間的區(qū)別)
                  • <i id='iSAnY'><tr id='iSAnY'><dt id='iSAnY'><q id='iSAnY'><span id='iSAnY'><b id='iSAnY'><form id='iSAnY'><ins id='iSAnY'></ins><ul id='iSAnY'></ul><sub id='iSAnY'></sub></form><legend id='iSAnY'></legend><bdo id='iSAnY'><pre id='iSAnY'><center id='iSAnY'></center></pre></bdo></b><th id='iSAnY'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='iSAnY'><tfoot id='iSAnY'></tfoot><dl id='iSAnY'><fieldset id='iSAnY'></fieldset></dl></div>
                      <bdo id='iSAnY'></bdo><ul id='iSAnY'></ul>
                      1. <small id='iSAnY'></small><noframes id='iSAnY'>

                        1. <tfoot id='iSAnY'></tfoot>
                          <legend id='iSAnY'><style id='iSAnY'><dir id='iSAnY'><q id='iSAnY'></q></dir></style></legend>
                              <tbody id='iSAnY'></tbody>

                            主站蜘蛛池模板: 黄色大片免费播放 | 久久专区| 五月婷婷在线视频 | 黄色一级大片在线免费看产 | 欧美日韩精选 | 久久久国产一区二区三区 | 九九热精品在线视频 | 日韩高清在线 | 亚洲精品无人区 | 91在线视频观看 | 中文字幕乱码一区二区三区 | 国产真实精品久久二三区 | av国产在线观看 | 日韩精品一区二区三区老鸭窝 | 久久久久一区二区三区 | 欧美中文在线 | 视频二区| 久久中文一区二区 | 伊人精品在线视频 | 日韩av电影在线观看 | 国产sm主人调教女m视频 | 日韩一三区 | 亚洲天堂久久新 | 911影院 | 国产一区二区精华 | 黄色成人免费在线观看 | 九九热热九九 | 亚洲成人一区 | 亚洲精品一区二三区不卡 | 国产精品久久久久久久免费大片 | 国产日产欧产精品精品推荐蛮挑 | 久草网免费 | 成人影院一区二区三区 | 又黄又色 | 久久久久国产成人精品亚洲午夜 | 国产精品一区久久久 | 中文字幕在线电影观看 | 成人免费在线 | 免费午夜视频在线观看 | www.欧美| 亚洲视频免费在线观看 |