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

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

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

    1. 我应该何时以及如何使用异常处理?

      时间:2023-08-03
      <tfoot id='T6R9h'></tfoot>

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

              <tbody id='T6R9h'></tbody>
              <legend id='T6R9h'><style id='T6R9h'><dir id='T6R9h'><q id='T6R9h'></q></dir></style></legend>

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

                问题描述

                我正在阅读有关异常处理的信息.我得到了一些关于什么是异常处理的信息,但我有几个问题:

                I am reading about exception handling. I got some information about what exception handling is, but I have a few questions:

                1. 什么时候抛出异常?
                2. 我们可以使用返回值来指示错误,而不是抛出异常吗?
                3. 如果我通过 try-catch 块保护我的所有函数,它会不会降低性能?
                4. 何时使用异常处理?
                5. 我看到一个项目,该项目中的每个函数都包含一个 try-catch 块(即整个函数内的代码被 try-catch 块包围).这是一个好的做法吗?
                6. try-catch 和 __try __except 有什么区别?

                推荐答案

                这是我认为必须阅读的关于异常的非常全面的指南:

                Here's quite comprehensive guide on exceptions that I think is a Must Read:

                异常和错误处理 - C++ FAQ 或 C++ FAQ lite

                Exceptions and error handling - C++ FAQ or C++ FAQ lite

                作为一般经验法则,当您的程序可以识别阻止执行的外部问题时,抛出异常.如果您从服务器接收数据并且该数据无效,则抛出异常.磁盘空间不足?抛出异常.宇宙射线阻止你查询数据库?抛出异常.但是如果你从你自己的程序中得到一些无效的数据——不要抛出异常.如果您的问题来自您自己的错误代码,最好使用 ASSERT 来防范它.需要异常处理来识别程序无法处理的问题并将其告知用户,因为用户可以处理它们.但是您的程序中的错误不是用户可以处理的,因此程序崩溃会告诉我们不少于answer_to_life_and_universe_and_everything 的值不是 42!这永远不会发生!!!!11"异常.

                As a general rule of thumb, throw an exception when your program can identify an external problem that prevents execution. If you receive data from the server and that data is invalid, throw an exception. Out of disk space? Throw an exception. Cosmic rays prevent you from querying the database? Throw an exception. But if you get some invalid data from inside your very own program - don't throw an exception. If your problem comes from your own bad code, it's better to use ASSERTs to guard against it. Exception handling is needed to identify problems that program cannot handle and tell them about the user, because user can handle them. But bugs in your program are not something the user can handle, so program crashing will tell not much less than "Value of answer_to_life_and_universe_and_everything is not 42! This should never happen!!!!11" exception.

                捕捉一个异常,你可以用它做一些有用的事情,比如显示一个消息框.我更喜欢在以某种方式处理用户输入的函数中捕获一次异常.例如,用户按下按钮歼灭所有 hunams",在 annihilateAllHunamsClicked() 函数中,有一个 try...catch 块来表示我不能".尽管消灭 hunamkind 是一项复杂的操作,需要调用数十个函数,但只有一次 try...catch,因为对于用户而言,这是一个原子操作 - 单击按钮.每个函数中的异常检查都是多余的和丑陋的.

                Catch an exception where you can do something useful with it, like, display a message box. I prefer to catch an exception once inside a function that somehow handles user input. For example, user presses button "Annihilate all hunams", and inside annihilateAllHunamsClicked() function there's a try...catch block to say "I can't". Even though annihilation of hunamkind is a complex operation that requires calling dozens and dozens of functions, there is only one try...catch, because for a user it's an atomic operation - one button click. Exception checks in every function are redundant and ugly.

                另外,我不能推荐足够熟悉 RAII - 也就是说,确保所有初始化的数据都被自动销毁.这可以通过尽可能多地在堆栈上初始化来实现,当您需要在堆上初始化某些内容时,请使用某种智能指针.当抛出异常时,栈上初始化的所有东西都会被自动销毁.如果你使用 C 风格的哑指针,当抛出异常时你会面临内存泄漏的风险,因为没有人在异常时清理它们(当然,你可以使用 C 风格的指针作为你的类的成员,但要确保它们是在析构函数中处理).

                Also, I can't recommend enough getting familiar with RAII - that is, to make sure that all data that is initialized is destroyed automatically. And that can be achieved by initializing as much as possible on stack, and when you need to initialize something on heap, use some kind of smart pointer. Everything initialized on the stack will be destroyed automatically when an exception is thrown. If you use C-style dumb pointers, you risk memory leak when an exception is thrown, because there is noone to clean them up upon exception (sure, you can use C-style pointers as members of your class, but make sure they are taken care of in destructor).

                这篇关于我应该何时以及如何使用异常处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:C++中异常对象的范围 下一篇:为什么要捕获异常作为对 const 的引用?

                相关文章

                最新文章

              • <tfoot id='e8QV6'></tfoot>

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

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