<legend id='WJTRu'><style id='WJTRu'><dir id='WJTRu'><q id='WJTRu'></q></dir></style></legend>

      <tfoot id='WJTRu'></tfoot>

        <bdo id='WJTRu'></bdo><ul id='WJTRu'></ul>
      1. <i id='WJTRu'><tr id='WJTRu'><dt id='WJTRu'><q id='WJTRu'><span id='WJTRu'><b id='WJTRu'><form id='WJTRu'><ins id='WJTRu'></ins><ul id='WJTRu'></ul><sub id='WJTRu'></sub></form><legend id='WJTRu'></legend><bdo id='WJTRu'><pre id='WJTRu'><center id='WJTRu'></center></pre></bdo></b><th id='WJTRu'></th></span></q></dt></tr></i><div id='WJTRu'><tfoot id='WJTRu'></tfoot><dl id='WJTRu'><fieldset id='WJTRu'></fieldset></dl></div>

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

      2. 在 C++ 中处理错误的正确方法是什么

        Whats the right approach for error handling in C++(在 C++ 中处理错误的正确方法是什么)
        <i id='Y6MF8'><tr id='Y6MF8'><dt id='Y6MF8'><q id='Y6MF8'><span id='Y6MF8'><b id='Y6MF8'><form id='Y6MF8'><ins id='Y6MF8'></ins><ul id='Y6MF8'></ul><sub id='Y6MF8'></sub></form><legend id='Y6MF8'></legend><bdo id='Y6MF8'><pre id='Y6MF8'><center id='Y6MF8'></center></pre></bdo></b><th id='Y6MF8'></th></span></q></dt></tr></i><div id='Y6MF8'><tfoot id='Y6MF8'></tfoot><dl id='Y6MF8'><fieldset id='Y6MF8'></fieldset></dl></div>
        • <legend id='Y6MF8'><style id='Y6MF8'><dir id='Y6MF8'><q id='Y6MF8'></q></dir></style></legend>
            • <tfoot id='Y6MF8'></tfoot>

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

                  <tbody id='Y6MF8'></tbody>

                  <bdo id='Y6MF8'></bdo><ul id='Y6MF8'></ul>
                  本文介绍了在 C++ 中处理错误的正确方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  一种是使用 C++ 异常:try catch 块.但是在引发异常时释放动态内存将是一个问题.

                  One is to use C++ exceptions: try catch blocks. But freeing dynamic memory will be an issue when an exception is raised.

                  二是使用C风格:errno变量

                  Second is to use C style: errno variable

                  第三个只是在错误时返回 -1,成功时返回 0 :)

                  Third is just to return -1 on error and 0 on success :)

                  中型项目应该选择哪种方式,为什么?还有其他更好的方法......?

                  Which way should be chosen for a mid-size project and why? Any other better approach..?

                  推荐答案

                  但在引发异常时释放动态内存将是一个问题.

                  But freeing dynamic memory will be an issue when an exception is raised.

                  不,不是.std::vectorv(100); 完成.

                  这里的概念称为范围绑定资源管理 (SBRM),也称为更常见(和笨拙)的名称资源获取即初始化 (RAII).基本上,所有资源都包含在某个对象中,该对象将清理析构函数中的资源(始终保证为自动分配的对象运行).因此,无论该函数是否正常存在或通过异常存在,都会运行析构函数并清理您的资源.

                  The concept here is called Scope-Bound Resource Management (SBRM), also known by the much more common (and awkward) name Resource Acquisition Is Initialization (RAII). Basically, all resources are contained in some object which will clean up the resource in the destructor (which is always guaranteed to be run for an automatically allocated object). So whether or not the function exists normally or via exception, the destructor is run and your resource is cleaned up.

                  永远不要在需要明确释放的地方进行分配,使用容器和智能指针.

                  Never do an allocation where you need to free it explicitly, use containers and smart pointers.

                  这篇关于在 C++ 中处理错误的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)
                • <tfoot id='IIvft'></tfoot>
                    <bdo id='IIvft'></bdo><ul id='IIvft'></ul>

                        <tbody id='IIvft'></tbody>

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

                        <legend id='IIvft'><style id='IIvft'><dir id='IIvft'><q id='IIvft'></q></dir></style></legend>

                          1. <i id='IIvft'><tr id='IIvft'><dt id='IIvft'><q id='IIvft'><span id='IIvft'><b id='IIvft'><form id='IIvft'><ins id='IIvft'></ins><ul id='IIvft'></ul><sub id='IIvft'></sub></form><legend id='IIvft'></legend><bdo id='IIvft'><pre id='IIvft'><center id='IIvft'></center></pre></bdo></b><th id='IIvft'></th></span></q></dt></tr></i><div id='IIvft'><tfoot id='IIvft'></tfoot><dl id='IIvft'><fieldset id='IIvft'></fieldset></dl></div>