pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

      When and how should I use exception handling?(我應(yīng)該何時以及如何使用異常處理?)
      <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="5hfnjpl" 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. 本文介紹了我應(yīng)該何時以及如何使用異常處理?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

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

                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 塊保護(hù)我的所有函數(shù),它會不會降低性能?
                4. 何時使用異常處理?
                5. 我看到一個項目,該項目中的每個函數(shù)都包含一個 try-catch 塊(即整個函數(shù)內(nèi)的代碼被 try-catch 塊包圍).這是一個好的做法嗎?
                6. try-catch 和 __try __except 有什么區(qū)別?

                推薦答案

                這是我認(rèn)為必須閱讀的關(guān)于異常的非常全面的指南:

                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

                作為一般經(jīng)驗法則,當(dāng)您的程序可以識別阻止執(zhí)行的外部問題時,拋出異常.如果您從服務(wù)器接收數(shù)據(jù)并且該數(shù)據(jù)無效,則拋出異常.磁盤空間不足?拋出異常.宇宙射線阻止你查詢數(shù)據(jù)庫?拋出異常.但是如果你從你自己的程序中得到一些無效的數(shù)據(jù)——不要拋出異常.如果您的問題來自您自己的錯誤代碼,最好使用 ASSERT 來防范它.需要異常處理來識別程序無法處理的問題并將其告知用戶,因為用戶可以處理它們.但是您的程序中的錯誤不是用戶可以處理的,因此程序崩潰會告訴我們不少于answer_to_life_and_universe_and_everything 的值不是 42!這永遠(yuǎn)不會發(fā)生!!!!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.

                捕捉一個異常,你可以用它做一些有用的事情,比如顯示一個消息框.我更喜歡在以某種方式處理用戶輸入的函數(shù)中捕獲一次異常.例如,用戶按下按鈕殲滅所有 hunams",在 annihilateAllHunamsClicked() 函數(shù)中,有一個 try...catch 塊來表示我不能".盡管消滅 hunamkind 是一項復(fù)雜的操作,需要調(diào)用數(shù)十個函數(shù),但只有一次 try...catch,因為對于用戶而言,這是一個原子操作 - 單擊按鈕.每個函數(shù)中的異常檢查都是多余的和丑陋的.

                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 - 也就是說,確保所有初始化的數(shù)據(jù)都被自動銷毀.這可以通過盡可能多地在堆棧上初始化來實現(xiàn),當(dāng)您需要在堆上初始化某些內(nèi)容時,請使用某種智能指針.當(dāng)拋出異常時,棧上初始化的所有東西都會被自動銷毀.如果你使用 C 風(fēng)格的啞指針,當(dāng)拋出異常時你會面臨內(nèi)存泄漏的風(fēng)險,因為沒有人在異常時清理它們(當(dāng)然,你可以使用 C 風(fēng)格的指針作為你的類的成員,但要確保它們是在析構(gòu)函數(shù)中處理).

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

                這篇關(guān)于我應(yīng)該何時以及如何使用異常處理?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                相關(guān)文檔推薦

                In what ways do C++ exceptions slow down code when there are no exceptions thown?(當(dāng)沒有異常時,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(從構(gòu)造函數(shù)的初始化列表中捕獲異常)
                Difference between C++03 throw() specifier C++11 noexcept(C++03 throw() 說明符 C++11 noexcept 之間的區(qū)別)
                Should the exception thrown by boost::asio::io_service::run() be caught?(應(yīng)該捕獲 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="rttfl7v" 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>

                          主站蜘蛛池模板: 门禁卡_智能IC卡_滴胶卡制作_硅胶腕带-卡立方rfid定制厂家 | 烽火安全网_加密软件、神盾软件官网 | 变色龙云 - 打包app_原生app_在线制作平台_短链接_ip查询 | 艺术涂料|木纹漆施工|稻草漆厂家|马来漆|石桦奴|水泥漆|选加河南天工涂料 | 雷蒙磨,雷蒙磨粉机,雷蒙磨机 - 巩义市大峪沟高峰机械厂 | 找果网 | 苹果手机找回方法,苹果iPhone手机丢了找回,认准找果网! | 济南画室培训-美术高考培训-山东艺霖艺术培训画室 | ASA膜,ASA共挤料,篷布色母料-青岛未来化学有限公司 | 湖南自考_湖南自学考试| 合肥活动房_安徽活动板房_集成打包箱房厂家-安徽玉强钢结构集成房屋有限公司 | 塑胶跑道_学校塑胶跑道_塑胶球场_运动场材料厂家_中国塑胶跑道十大生产厂家_混合型塑胶跑道_透气型塑胶跑道-广东绿晨体育设施有限公司 | 托利多电子平台秤-高精度接线盒-托利多高精度电子秤|百科 | wika威卡压力表-wika压力变送器-德国wika代理-威卡总代-北京博朗宁科技 | 水性漆|墙面漆|木器家具漆|水漆涂料_晨阳水漆官网 | 溶氧传感器-pH传感器|哈美顿(hamilton) | 体检车_移动CT车_CT检查车_CT车_深圳市艾克瑞电气有限公司移动CT体检车厂家-深圳市艾克瑞电气有限公司 | 机构创新组合设计实验台_液压实验台_气动实训台-戴育教仪厂 | 安全光栅|射频导纳物位开关|音叉料位计|雷达液位计|两级跑偏开关|双向拉绳开关-山东卓信机械有限公司 | 创客匠人-让IP变现不走弯路| 一级建造师培训_一建培训机构_中建云筑建造师培训网校 | 工业洗衣机_工业洗涤设备_上海力净工业洗衣机厂家-洗涤设备首页 bkzzy在职研究生网 - 在职研究生招生信息咨询平台 | 高压无油空压机_无油水润滑空压机_水润滑无油螺杆空压机_无油空压机厂家-科普柯超滤(广东)节能科技有限公司 | 江西自考网 | 博莱特空压机|博莱特-阿特拉斯独资空压机品牌核心代理商 | 旋振筛|圆形摇摆筛|直线振动筛|滚筒筛|压榨机|河南天众机械设备有限公司 | 云杂志网-学术期刊-首页 | 企业微信scrm管理系统_客户关系管理平台_私域流量运营工具_CRM、ERP、OA软件-腾辉网络 | 玻璃瓶厂家_酱菜瓶厂家_饮料瓶厂家_酒瓶厂家_玻璃杯厂家_徐州东明玻璃制品有限公司 | 无线联网门锁|校园联网门锁|学校智能门锁|公租房智能门锁|保障房管理系统-KEENZY中科易安 | 超声骨密度仪-动脉硬化检测仪器-人体成分分析仪厂家/品牌/价格_南京科力悦 | 深圳市索富通实业有限公司-可燃气体报警器 | 可燃气体探测器 | 气体检测仪 | 定做大型恒温循环水浴槽-工业用不锈钢恒温水箱-大容量低温恒温水槽-常州精达仪器 | 转向助力泵/水泵/发电机皮带轮生产厂家-锦州华一精工有限公司 | 天津试验仪器-电液伺服万能材料试验机,恒温恒湿标准养护箱,水泥恒应力压力试验机-天津鑫高伟业科技有限公司 | 仓储笼_仓储货架_南京货架_仓储货架厂家_南京货架价格低-南京一品仓储设备制造公司 | 浙江建筑资质代办_二级房建_市政_电力_安许_劳务资质办理公司 | 合肥防火门窗/隔断_合肥防火卷帘门厂家_安徽耐火窗_良万消防设备有限公司 | 稳尚教育加盟-打造高考志愿填报平台_新高考志愿填报加盟_学业生涯规划加盟 | 伸缩节_伸缩器_传力接头_伸缩接头_巩义市联通管道厂 | 济南铝方通-济南铝方通价格-济南方通厂家-山东鲁方通建材有限公司 | 热镀锌槽钢|角钢|工字钢|圆钢|H型钢|扁钢|花纹板-天津千百顺钢铁贸易有限公司 |