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

<tfoot id='T6R9h'></tfoot>

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

    1. <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 class="qwawimqqmiuu" id='T6R9h'><tfoot id='T6R9h'></tfoot><dl id='T6R9h'><fieldset id='T6R9h'></fieldset></dl></div>
        <bdo id='T6R9h'></bdo><ul id='T6R9h'></ul>
    2. 我應該何時以及如何使用異常處理?

      When and how should I use exception handling?(我應該何時以及如何使用異常處理?)
      <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 class="qwawimqqmiuu" 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>
            <tbody id='kLrCa'></tbody>

        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. 本文介紹了我應該何時以及如何使用異常處理?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我正在閱讀有關異常處理的信息.我得到了一些關于什么是異常處理的信息,但我有幾個問題:

                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模板網!

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

                相關文檔推薦

                In what ways do C++ exceptions slow down code when there are no exceptions thown?(當沒有異常時,C++ 異常會以何種方式減慢代碼速度?)
                Why catch an exception as reference-to-const?(為什么要捕獲異常作為對 const 的引用?)
                Scope of exception object in C++(C++中異常對象的范圍)
                Catching exceptions from a constructor#39;s initializer list(從構造函數的初始化列表中捕獲異常)
                Difference between C++03 throw() specifier C++11 noexcept(C++03 throw() 說明符 C++11 noexcept 之間的區別)
                Should the exception thrown by boost::asio::io_service::run() be caught?(應該捕獲 boost::asio::io_service::run() 拋出的異常嗎?)
                • <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 class="qwawimqqmiuu" id='e8QV6'><tfoot id='e8QV6'></tfoot><dl id='e8QV6'><fieldset id='e8QV6'></fieldset></dl></div>
                      <tbody id='e8QV6'></tbody>
                    <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>

                          主站蜘蛛池模板: 国产日韩一区二区三免费高清 | 中文字字幕一区二区三区四区五区 | 国产精品日韩欧美一区二区 | 日本高清中文字幕 | 综合激情久久 | 亚洲国产成人精品女人久久久 | 人人色视频| 亚洲精品国产电影 | 精品一区二区av | 国产美女在线精品免费 | 久久精品国产一区二区三区不卡 | 免费一级黄色 | 在线观看亚洲 | 日韩一区二区三区在线播放 | 欧美不卡 | 二区在线视频 | 午夜网站视频 | 色婷婷av一区二区三区软件 | 黑人巨大精品欧美一区二区免费 | 日韩在线观看中文字幕 | 精品久久久久久国产 | 在线午夜电影 | av第一页 | 免费看一区二区三区 | 黄色av网站在线免费观看 | 综合精品在线 | 日日日干干干 | 久久久精品一区 | 性色av香蕉一区二区 | 欧美日韩国产一区二区三区 | 久久久久国产视频 | 久久久久久久久久久久久久久久久久久久 | 色吧久久 | 91国内视频在线 | 福利视频一二区 | 欧美综合一区二区三区 | 亚洲精品乱码久久久久久蜜桃 | 国产精品一区三区 | 成人在线a| aaa精品 | 精品视频一区二区三区在线观看 |