問(wèn)題描述
我在 PHP 的 Zend 框架中為我的應(yīng)用程序編寫(xiě)了許多小型庫(kù)(一堆類).我也一直在庫(kù)的方法本身中捕獲這些異常并將它們記錄到文件中.
I had been writing number of small libraries (bunch of classes) for my application inside PHP's Zend Framework. I had also been catching these exceptions inside the library's methods itself and logging them to a file.
然后突然我遇到了一個(gè)問(wèn)題,即使用這些庫(kù)的主應(yīng)用程序即使在我預(yù)計(jì)它們會(huì)因致命錯(cuò)誤而退出的情況下也不會(huì)因錯(cuò)誤而退出.這樣做的問(wèn)題是下面的代碼一直執(zhí)行到最后——它不應(yīng)該有.
Then suddenly I ran to an issue that my main application that was using these libraries would not quit due to errors even in situations I expected them to quit due to a fatal error. The problem with this was the code below kept executing till the end - which it should not have.
捕獲并記錄庫(kù)類中的大多數(shù)錯(cuò)誤(特殊情況除外)似乎不是一個(gè)好習(xí)慣.他們應(yīng)該總是按原樣拋出錯(cuò)誤?這是一個(gè)正確的假設(shè)嗎?
It seems like its not a good practice to catch and perhaps log majority (except in special cases) of the errors inside the library classes. They should always throw the error as it is? Would that be a correct assumption?
如果有人能為我回答這個(gè)問(wèn)題,我將不勝感激.
I'd appreciate if anyone could answer this for me.
推薦答案
在任何語(yǔ)言中,異常的一般哲學(xué)是它們傳達(dá)異常情況.您應(yīng)該相應(yīng)地使用它們.
The general philosophy of exceptions, in any language, is that they communicate exceptional circumstances. You should use them accordingly.
如果你最終用一個(gè) try
塊來(lái)包圍每個(gè)函數(shù)調(diào)用,那就有問(wèn)題了.異常被精確地設(shè)計(jì)為使錯(cuò)誤處理合乎邏輯,并且不需要程序員跟蹤所有可能的錯(cuò)誤路徑.因此,您應(yīng)該在可以有意義地對(duì)異常做出響應(yīng)的那些點(diǎn)捕獲異常.
If you end up surrounding every function call with a try
block, something is wrong. Exceptions are precisely designed to make error handling logical and not require the programmer to track all possible error paths. Therefore, you should catch exceptions precisely at those points where you can respond meaningfully to them.
如果您想不出比中止和傳播錯(cuò)誤更好的方法,那么捕獲異常就沒(méi)有意義了.另一方面,如果您能夠?qū)δ承╁e(cuò)誤做出明智的反應(yīng),請(qǐng)抓住它們,然后重新拋出任何其他錯(cuò)誤.
If you cannot think of anything better to do than to abort and propagate the error, then there's no point catching an exception. On the other hand, if there are some errors to which you can react sensibly, catch those, and rethrow anything else.
一個(gè)典型的例子是,如果您要處理大量文件.如果解析邏輯中的任何地方出現(xiàn)錯(cuò)誤,您就無(wú)能為力,即使解析可能會(huì)中斷許多函數(shù)調(diào)用.但是,在主循環(huán)中,您可以嘗試
解析每個(gè)文件,如果有異常,您可以捕獲該異常,跳過(guò)該文件并繼續(xù)下一個(gè).
A typical example is if you're processing lots of files. If there's an error anywhere inside the parsing logic, there's nothing you can do, even though parsing may go down many function calls. However, at the main loop you can try
parsing each file, and if there's an exception, you catch that, skip the file and continue with the next one.
如果您正在編寫(xiě)一個(gè)庫(kù)函數(shù),您可能希望在整個(gè)函數(shù)周圍有一個(gè)最后的 try 塊;不過(guò),這在某種程度上取決于您.只需清楚地記錄用戶必須從您的庫(kù)中獲得哪些例外.
If you're writing a library function, you might want to have one final try block surrounding your entire function; that's somewhat up to you, though. Just document cleanly which exceptions the user has to expect from your library.
這篇關(guān)于異常捕獲:什么時(shí)候不捕獲它們?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!