• <small id='UQQQM'></small><noframes id='UQQQM'>

    1. <i id='UQQQM'><tr id='UQQQM'><dt id='UQQQM'><q id='UQQQM'><span id='UQQQM'><b id='UQQQM'><form id='UQQQM'><ins id='UQQQM'></ins><ul id='UQQQM'></ul><sub id='UQQQM'></sub></form><legend id='UQQQM'></legend><bdo id='UQQQM'><pre id='UQQQM'><center id='UQQQM'></center></pre></bdo></b><th id='UQQQM'></th></span></q></dt></tr></i><div id='UQQQM'><tfoot id='UQQQM'></tfoot><dl id='UQQQM'><fieldset id='UQQQM'></fieldset></dl></div>
      <legend id='UQQQM'><style id='UQQQM'><dir id='UQQQM'><q id='UQQQM'></q></dir></style></legend>
        <bdo id='UQQQM'></bdo><ul id='UQQQM'></ul>
      1. <tfoot id='UQQQM'></tfoot>
      2. 如何捕捉空指针异常?

        时间:2023-08-03
      3. <tfoot id='X9VS8'></tfoot>

            <tbody id='X9VS8'></tbody>

            • <legend id='X9VS8'><style id='X9VS8'><dir id='X9VS8'><q id='X9VS8'></q></dir></style></legend>
                <bdo id='X9VS8'></bdo><ul id='X9VS8'></ul>

                1. <small id='X9VS8'></small><noframes id='X9VS8'>

                  <i id='X9VS8'><tr id='X9VS8'><dt id='X9VS8'><q id='X9VS8'><span id='X9VS8'><b id='X9VS8'><form id='X9VS8'><ins id='X9VS8'></ins><ul id='X9VS8'></ul><sub id='X9VS8'></sub></form><legend id='X9VS8'></legend><bdo id='X9VS8'><pre id='X9VS8'><center id='X9VS8'></center></pre></bdo></b><th id='X9VS8'></th></span></q></dt></tr></i><div id='X9VS8'><tfoot id='X9VS8'></tfoot><dl id='X9VS8'><fieldset id='X9VS8'></fieldset></dl></div>
                  本文介绍了如何捕捉空指针异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                    try {
                          int* p = 0;
                          *p = 1;
                      } catch (...) {
                          cout << "null pointer." << endl;
                      }
                  

                  我试图捕捉这样的异常,但它不起作用,有什么帮助吗?

                  I tried to catch the exception like this but it doesn't work,any help?

                  推荐答案

                  C++ 中没有空指针异常"这样的东西.您可以捕获的唯一异常是 throw 表达式显式抛出的异常(另外,正如 Pavel 所指出的,一些标准 C++ 异常由标准 operator newdynamic_cast 等).C++ 中没有其他例外.取消引用空指针、除以零等不会在 C++ 中产生异常,它会产生未定义行为.如果您希望在这种情况下抛出异常,您有责任手动检测这些条件并明确地执行 throw.这就是它在 C++ 中的工作方式.

                  There's no such thing as "null pointer exception" in C++. The only exceptions you can catch, is the exceptions explicitly thrown by throw expressions (plus, as Pavel noted, some standard C++ exceptions thrown intrinsically by standard operator new, dynamic_cast etc). There are no other exceptions in C++. Dereferencing null pointers, division by zero etc. does not generate exceptions in C++, it produces undefined behavior. If you want exceptions thrown in cases like that it is your own responsibility to manually detect these conditions and do throw explicitly. That's how it works in C++.

                  您似乎正在寻找的其他任何东西都与 C++ 语言无关,而是特定实现的功能.例如,在 Visual C++ 中,系统/硬件异常可以转换"为 C++ 异常,但这种非标准功能是有代价的,通常不值得付出代价.

                  Whatever else you seem to be looking for has noting to do with C++ language, but rather a feature of particular implementation. In Visual C++, for example, system/hardware exceptions can be "converted" into C++ exceptions, but there's a price attached to this non-standard functionality, which is not normally worth paying.

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

                  上一篇:C++ 捕获所有异常 下一篇:谁删除了在“新建"期间分配的内存?构造函数

                  相关文章

                  最新文章

                2. <tfoot id='RGjIs'></tfoot>
                  <i id='RGjIs'><tr id='RGjIs'><dt id='RGjIs'><q id='RGjIs'><span id='RGjIs'><b id='RGjIs'><form id='RGjIs'><ins id='RGjIs'></ins><ul id='RGjIs'></ul><sub id='RGjIs'></sub></form><legend id='RGjIs'></legend><bdo id='RGjIs'><pre id='RGjIs'><center id='RGjIs'></center></pre></bdo></b><th id='RGjIs'></th></span></q></dt></tr></i><div id='RGjIs'><tfoot id='RGjIs'></tfoot><dl id='RGjIs'><fieldset id='RGjIs'></fieldset></dl></div>
                  1. <legend id='RGjIs'><style id='RGjIs'><dir id='RGjIs'><q id='RGjIs'></q></dir></style></legend>
                  2. <small id='RGjIs'></small><noframes id='RGjIs'>

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