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

      <legend id='9e1Cy'><style id='9e1Cy'><dir id='9e1Cy'><q id='9e1Cy'></q></dir></style></legend>

        <small id='9e1Cy'></small><noframes id='9e1Cy'>

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

        • <bdo id='9e1Cy'></bdo><ul id='9e1Cy'></ul>

        為什么要保守地使用異常?

        Why should exceptions be used conservatively?(為什么要保守地使用異常?)

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

              <tbody id='n0OV9'></tbody>

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

              <bdo id='n0OV9'></bdo><ul id='n0OV9'></ul>
              <legend id='n0OV9'><style id='n0OV9'><dir id='n0OV9'><q id='n0OV9'></q></dir></style></legend>
                • 本文介紹了為什么要保守地使用異常?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問題描述

                  我經(jīng)常看到/聽到人們說異常應(yīng)該很少使用,但從不解釋原因.雖然這可能是真的,但基本原理通常是一個(gè)油嘴滑舌:它被稱為異常是有原因的",對(duì)我來(lái)說,這似乎是一種不應(yīng)該被受人尊敬的程序員接受的解釋/工程師.

                  I often see/hear people say that exceptions should only be used rarely, but never explain why. While that may be true, rationale is normally a glib: "it's called an exception for a reason" which, to me, seems to be the sort of explanation that should never be accepted by a respectable programmer/engineer.

                  異常可用于解決一系列問題.為什么將它們用于控制流是不明智的?對(duì)它們的使用方式異常保守的背后的哲學(xué)是什么?語(yǔ)義?表現(xiàn)?復(fù)雜?美學(xué)?約定?

                  There is a range of problems that an exception can be used to solve. Why is it unwise to use them for control flow? What is the philosophy behind being exceptionally conservative with how they are used? Semantics? Performance? Complexity? Aesthetics? Convention?

                  我之前看過一些關(guān)于性能的分析,但在與某些系統(tǒng)相關(guān)而與其他系統(tǒng)無(wú)關(guān)的層面上.

                  I've seen some analysis on performance before, but at a level that would be relevant to some systems and irrelevant to others.

                  同樣,我不一定不同意它們應(yīng)該在特殊情況下保存,但我想知道共識(shí)的基本原理是什么(如果存在這樣的事情).

                  Again, I don't necessarily disagree that they should be saved for special circumstances, but I'm wondering what the consensus rationale is (if such a thing exists).

                  推薦答案

                  主要的摩擦點(diǎn)是語(yǔ)義.許多開發(fā)人員濫用異常并抓住一切機(jī)會(huì)拋出它們.這個(gè)想法是在一些特殊情況下使用異常.例如,錯(cuò)誤的用戶輸入不算作異常,因?yàn)槟M@會(huì)發(fā)生并為此做好準(zhǔn)備.但是,如果您嘗試創(chuàng)建一個(gè)文件并且磁盤上沒有足夠的空間,那么是的,這是一個(gè)明顯的例外.

                  The primary point of friction is semantics. Many developers abuse exceptions and throw them at every opportunity. The idea is to use exception for somewhat exceptional situation. For example, wrong user input does not count as an exception because you expect this to happen and ready for that. But if you tried to create a file and there was not enough space on disk, then yes, this is a definite exception.

                  另一個(gè)問題是異常經(jīng)常被拋出和吞下.開發(fā)人員使用這種技術(shù)來(lái)簡(jiǎn)單地靜默"程序并讓它運(yùn)行盡可能長(zhǎng)的時(shí)間直到完全崩潰.這是非常錯(cuò)誤的.如果你不處理異常,如果你沒有通過釋放一些資源來(lái)做出適當(dāng)?shù)姆磻?yīng),如果你沒有記錄異常發(fā)生或至少?zèng)]有通知用戶,那么你就沒有使用異常的含義.

                  One other issue is that exceptions are often thrown and swallowed. Developers use this technique to simply "silence" the program and let it run as long as possible until completely collapsing. This is very wrong. If you don't process exceptions, if you don't react appropriately by freeing some resources, if you don't log the exception occurrence or at least not notify the user, then you're not using exception for what they are meant.

                  直接回答你的問題.應(yīng)該很少使用異常,因?yàn)楫惓G闆r很少見,而且異常代價(jià)高昂.

                  Answering directly your question. Exceptions should rarely be used because exceptional situations are rare and exceptions are expensive.

                  很少見,因?yàn)槟幌M看伟聪掳粹o或每次輸入格式錯(cuò)誤時(shí)程序都會(huì)崩潰.比如說,數(shù)據(jù)庫(kù)可能突然無(wú)法訪問,可能磁盤空間不足,您依賴的某些第三方服務(wù)離線,這一切都可能發(fā)生,但很少見,這些都是明顯的例外情況.

                  Rare, because you don't expect your program crash at every button press or at every malformed user input. Say, database may suddenly not be accessible, there may not be enough space on disk, some third party service you depend on is offline, this all can happen, but quite rarely, these would be clear exceptional cases.

                  昂貴,因?yàn)閽伋霎惓?huì)中斷正常的程序流程.運(yùn)行時(shí)將展開堆棧,直到找到可以處理異常的適當(dāng)異常處理程序.它還將在整個(gè)過程中收集調(diào)用信息以傳遞給處理程序?qū)⒔邮盏漠惓?duì)象.都是有代價(jià)的.

                  Expensive, because throwing an exception will interrupt the normal program flow. The runtime will unwind the stack until it finds an appropriate exception handler that can handle the exception. It will also gather the call information all along the way to be passed to the exception object the handler will receive. It all has costs.

                  這并不是說使用異常就沒有例外(微笑).有時(shí),如果您拋出異常而不是通過多層轉(zhuǎn)發(fā)返回代碼,它可以簡(jiǎn)化代碼結(jié)構(gòu).作為一個(gè)簡(jiǎn)單的規(guī)則,如果您希望某個(gè)方法經(jīng)常被調(diào)用并且在一半的時(shí)間里發(fā)現(xiàn)了一些異常"情況,那么最好找到另一個(gè)解決方案.但是,如果您希望大部分時(shí)間都可以正常運(yùn)行,而這種異常"情況只會(huì)出現(xiàn)在極少數(shù)情況下,那么拋出異常就可以了.

                  This is not to say that there can be no exception to using exceptions (smile). Sometimes it can simplify the code structure if you throw an exception instead of forwarding return codes via many layers. As a simple rule, if you expect some method to be called often and discover some "exceptional" situation half the time then it is better to find another solution. If however you expect normal flow of operation most of the time while this "exceptional" situation can only emerge in some rare circumstances, then it is just fine to throw an exception.

                  @Comments:如果可以讓您的代碼更簡(jiǎn)單、更容易,則絕對(duì)可以在一些不太例外的情況下使用異常.這個(gè)選項(xiàng)是開放的,但我想說它在實(shí)踐中很少見.

                  @Comments: Exception can definitely be used in some less-exceptional situations if that could make your code simpler and easier. This option is open but I'd say it comes quite rare in practice.

                  為什么將它們用于控制流是不明智的?

                  Why is it unwise to use them for control flow?

                  因?yàn)楫惓?huì)破壞正常的控制流".您引發(fā)異常并放棄程序的正常執(zhí)行,這可能會(huì)導(dǎo)致對(duì)象處于不一致狀態(tài),并且某些開放資源未釋放.當(dāng)然,C# 有 using 語(yǔ)句,它可以確保即使從 using 主體拋出異常,對(duì)象也會(huì)被釋放.但是讓我們暫時(shí)從語(yǔ)言中抽象出來(lái).假設(shè)框架不會(huì)為您處理對(duì)象.你手動(dòng)完成.你有一些關(guān)于如何請(qǐng)求和釋放資源和內(nèi)存的系統(tǒng).您在系統(tǒng)范圍內(nèi)達(dá)成一致,誰(shuí)負(fù)責(zé)在什么情況下釋放對(duì)象和資源.您有如何處理外部庫(kù)的規(guī)則.如果程序遵循正常的操作流程,則效果很好.但是在執(zhí)行過程中突然你拋出一個(gè)異常.一半的資源沒有被釋放.一半還沒有被要求.如果該操作現(xiàn)在是事務(wù)性的,它就會(huì)被破壞.您處理資源的規(guī)則將不起作用,因?yàn)槟切┴?fù)責(zé)釋放資源的代碼部分根本不會(huì)執(zhí)行.如果其他人想要使用這些資源,他們可能會(huì)發(fā)現(xiàn)它們處于不一致的狀態(tài)并崩潰,因?yàn)樗麄儫o(wú)法預(yù)測(cè)這種特定情況.

                  Because exceptions disrupt normal "control flow". You raise an exception and normal execution of the program is abandoned potentially leaving objects in inconsistent state and some open resources unfreed. Sure, C# has the using statement which will make sure the object will be disposed even if an exception is thrown from the using body. But let us abstract for the moment from the language. Suppose the framework won't dispose objects for you. You do it manually. You have some system for how to request and free resources and memory. You have agreement system-wide who is responsible for freeing objects and resources in what situations. You have rules how to deal with external libraries. It works great if the program follows the normal operation flow. But suddenly in the middle of execution you throw an exception. Half of the resources are left unfreed. Half have not been requested yet. If the operation was meant to be transactional now it is broken. Your rules for handling resources will not work because those code parts responsible for freeing resources simply won't execute. If anybody else wanted to use those resources they may find them in inconsistent state and crash as well because they could not predict this particular situation.

                  比如說,你想要一個(gè)方法 M() 調(diào)用方法 N() 來(lái)做一些工作并安排一些資源,然后將它返回給 M(),它會(huì)使用它然后處理它.美好的.現(xiàn)在 N() 中出了點(diǎn)問題,它在 M() 中拋出了一個(gè)您沒有預(yù)料到的異常,因此異常冒泡到頂部,直到它可能被某個(gè)方法 C() 捕獲,而 C() 將不知道在深處發(fā)生了什么在 N() 中以及是否以及如何釋放一些資源.

                  Say, you wanted a method M() call method N() to do some work and arrange for some resource then return it back to M() which will use it and then dispose it. Fine. Now something goes wrong in N() and it throws an exception you didn't expect in M() so the exception bubbles to the top until it maybe gets caught in some method C() which will have no idea what was happening deep down in N() and whether and how to free some resources.

                  通過拋出異常,您可以創(chuàng)建一種方法,使您的程序進(jìn)入許多難以預(yù)測(cè)、理解和處理的新的不可預(yù)測(cè)的中間狀態(tài).這有點(diǎn)類似于使用 GOTO.設(shè)計(jì)一個(gè)程序可以將其執(zhí)行從一個(gè)位置隨機(jī)跳轉(zhuǎn)到另一個(gè)位置是非常困難的.它也很難維護(hù)和調(diào)試.當(dāng)程序變得越來(lái)越復(fù)雜時(shí),您只會(huì)失去對(duì)何時(shí)何地發(fā)生了什么事情的概覽,而無(wú)法修復(fù)它.

                  With throwing exceptions you create a way to bring your program into many new unpredictable intermediate states which are hard to prognose, understand and deal with. It's somewhat similar to using GOTO. It is very hard to design a program that can randomly jump its execution from one location to the other. It will also be hard to maintain and debug it. When the program grows in complexity, you just going to lose an overview of what when and where is happening less to fix it.

                  這篇關(guān)于為什么要保守地使用異常?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

                  In what ways do C++ exceptions slow down code when there are no exceptions thown?(當(dāng)沒有異常時(shí),C++ 異常會(huì)以何種方式減慢代碼速度?)
                  Why catch an exception as reference-to-const?(為什么要捕獲異常作為對(duì) const 的引用?)
                  When and how should I use exception handling?(我應(yīng)該何時(shí)以及如何使用異常處理?)
                  Scope of exception object in C++(C++中異常對(duì)象的范圍)
                  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ū)別)
                  <legend id='Y3T1V'><style id='Y3T1V'><dir id='Y3T1V'><q id='Y3T1V'></q></dir></style></legend>
                    <tbody id='Y3T1V'></tbody>

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

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

                          <bdo id='Y3T1V'></bdo><ul id='Y3T1V'></ul>
                            <i id='Y3T1V'><tr id='Y3T1V'><dt id='Y3T1V'><q id='Y3T1V'><span id='Y3T1V'><b id='Y3T1V'><form id='Y3T1V'><ins id='Y3T1V'></ins><ul id='Y3T1V'></ul><sub id='Y3T1V'></sub></form><legend id='Y3T1V'></legend><bdo id='Y3T1V'><pre id='Y3T1V'><center id='Y3T1V'></center></pre></bdo></b><th id='Y3T1V'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Y3T1V'><tfoot id='Y3T1V'></tfoot><dl id='Y3T1V'><fieldset id='Y3T1V'></fieldset></dl></div>
                          1. 主站蜘蛛池模板: 亚洲精品一级 | 人人叉| 中文字幕免费视频 | 四虎成人在线播放 | 2019天天干夜夜操 | 国产精品久久久久久吹潮 | 精品亚洲一区二区三区 | 亚洲成人精品在线观看 | 国产精品国产a | 综合国产第二页 | 亚洲不卡在线视频 | 成人免费一区二区三区视频网站 | 久久久久久久久久久久久91 | 中文亚洲视频 | 天天躁日日躁狠狠躁白人 | 男人天堂手机在线视频 | 成人在线不卡 | 精品久久久久久久久久久久 | 亚洲综合成人网 | 免费精品视频在线观看 | 国产乱码精品一区二区三区五月婷 | 国产情侣啪啪 | 在线中文字幕国产 | 在线欧美亚洲 | 国产免费xxx | www.亚洲国产精品 | 日韩高清一区二区 | 喷潮网站 | 久久91精品国产一区二区三区 | 97精品一区二区 | 免费在线性爱视频 | 久久国产精品久久国产精品 | 久久久久久成人 | 久久一区二区视频 | 国产精品免费大片 | 人人干在线视频 | 日本一区二区不卡 | 精品久 | 91在线网站| 免费视频一区二区 | 午夜久久久久 |