問題描述
關于 PHP 中的錯誤處理——據我所知有 3 種樣式:
In regards to Error handling in PHP -- As far I know there are 3 styles:
die()
或exit()
樣式:
$con = mysql_connect("localhost","root","password");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
拋出異常
樣式:
if (!function_exists('curl_init')) {
throw new Exception('need the CURL PHP extension.
Recomplie PHP with curl');
}
trigger_error()
樣式:
if(!is_array($config) && isset($config)) {
trigger_error('Error: config is not an array or is not set', E_USER_ERROR);
}
現在,在 PHP 手冊中,所有三種方法都使用了.
Now, in the PHP manual all three methods are used.
我想知道我應該更喜歡哪種風格&為什么?
What I want to know is which style should I prefer & why?
這 3 種是相互替代嗎?因此可以互換使用嗎?
Are these 3 drop in replacements of each other & therefore can be used interchangeably?
有點 OT:是我還是所有人都認為 PHP 錯誤處理選項太多以至于讓 php 開發人員感到困惑?
Slightly OT: Is it just me or everyone thinks PHP error handling options are just too many to the extent it confuses php developers?
推薦答案
第一個不應該在生產代碼中使用,因為它傳輸與最終用戶無關的信息(用戶無法對 做任何事情"無法連接到數據庫").
The first one should never be used in production code, since it's transporting information irrelevant to end-users (a user can't do anything about "Cannot connect to database").
如果您知道在某個關鍵代碼點,您的應用程序可能會失敗,并且您希望您的代碼在多個調用級別之間恢復,那么您就會拋出異常.
You throw Exceptions if you know that at a certain critical code point, your application can fail and you want your code to recover across multiple call-levels.
trigger_error()
可讓您細粒度的錯誤報告(通過使用不同級別的錯誤消息)并且您可以對最終用戶隱藏這些錯誤(使用 set_error_handler()
) 但在測試過程中仍然顯示給你.
trigger_error()
lets you fine-grain error reporting (by using different levels of error messages) and you can hide those errors from end-users (using set_error_handler()
) but still have them be displayed to you during testing.
此外,trigger_error()
可以生成在開發過程中很重要的非致命消息,可以使用自定義錯誤處理程序在生產代碼中抑制這些消息.您也可以產生致命錯誤 (E_USER_ERROR
),但這些錯誤是不可恢復的.如果您觸發其中之一,程序執行將在該點停止.這就是為什么,對于致命錯誤,應該使用異常.這樣,您就可以更好地控制程序的流程:
Also trigger_error()
can produce non-fatal messages important during development that can be suppressed in production code using a custom error handler. You can produce fatal errors, too (E_USER_ERROR
) but those aren't recoverable. If you trigger one of those, program execution stops at that point. This is why, for fatal errors, Exceptions should be used. This way, you'll have more control over your program's flow:
// Example (pseudo-code for db queries):
$db->query('START TRANSACTION');
try {
while ($row = gather_data()) {
$db->query('INSERT INTO `table` (`foo`,`bar`) VALUES(?,?)', ...);
}
$db->query('COMMIT');
} catch(Exception $e) {
$db->query('ROLLBACK');
}
在這里,如果 gather_data()
只是簡單的嘶嘶聲(使用 E_USER_ERROR
或 die()
),則有機會,之前的 INSERT
語句會將其添加到您的數據庫中,即使不需要,您也無法控制接下來會發生什么.
Here, if gather_data()
just plain croaked (using E_USER_ERROR
or die()
) there's a chance, previous INSERT
statements would have made it into your database, even if not desired and you'd have no control over what's to happen next.
這篇關于PHP 錯誤處理:die() Vs trigger_error() Vs throw Exception的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!