• <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 id='NOkgc'><tfoot id='NOkgc'></tfoot><dl id='NOkgc'><fieldset id='NOkgc'></fieldset></dl></div>

      1. 如何抛出 C++ 异常

        时间:2023-08-03

          <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 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>
                  <tbody id='dW4Mz'></tbody>
                • <tfoot id='dW4Mz'></tfoot>
                  本文介绍了如何抛出 C++ 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我对异常处理的理解很差(即,如何为自己的目的自定义 throw、try、catch 语句).

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

                  例如我定义了一个函数如下:int compare(int a, int b){...}

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

                  我希望函数在 a 或 b 为负数时抛出一个带有一些消息的异常.

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

                  我应该如何在函数定义中解决这个问题?

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

                  推荐答案

                  简单:

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

                  标准库附带了一个很好的内置异常对象你可以扔.请记住,您应该始终按值抛出并按引用捕获:

                  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... 
                  }
                  

                  您可以在每次尝试后使用多个 catch() 语句,因此您可以根据需要分别处理不同的异常类型.

                  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( ... ) { };
                  

                  这篇关于如何抛出 C++ 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++中的对象销毁 下一篇:C++ catch 块 - 按值或引用捕获异常?

                  相关文章

                  最新文章

                • <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 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>