問題描述
我正在閱讀有關異常處理的信息.我得到了一些關于什么是異常處理的信息,但我有幾個問題:
I am reading about exception handling. I got some information about what exception handling is, but I have a few questions:
- 什么時候拋出異常?
- 我們可以使用返回值來指示錯誤,而不是拋出異常嗎?
- 如果我通過 try-catch 塊保護我的所有函數,它會不會降低性能?
- 何時使用異常處理?
- 我看到一個項目,該項目中的每個函數都包含一個 try-catch 塊(即整個函數內的代碼被 try-catch 塊包圍).這是一個好的做法嗎?
- 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模板網!