我想用我的自定義錯誤消息自定義異常錯誤按如下:PHP - 自定義異常錯誤
try {
$test = 1/0;
}
catch(Exception $ex){
$t=time();
$timelog = date("dmYhms",$t);
error_log($timelog."=> ErrorID: ".$timelog."\n", 3, "../error_log/Error.log");
echo "System having issue with this request.\n
Transaction ID: ".$timelog;
}
我的期望是,我要echo
管理的自定義消息而不是默認的消息,我設法寫入日誌文件。但是,我仍然獲得了默認的錯誤信息,而不是下面的圖片中的自定義錯誤信息。
任何想法,我做了錯誤。請指教。
EDITED
error_reporting(0);
//error handler function
function customError($errno, $errstr) {
//echo "<b>Error:</b> [$errno] $errstr<br>";
$t=time();
$timelog = date("dmYhms",$t);
echo "Unable to process with the request. \n Error ID".$timelog.". PLease contact RND team.";
error_log($timelog."=> Error: [$errno] $errstr\n", 3, "error_log/Error.log");
}
//set error handler
set_error_handler("customError",E_USER_WARNING);
//trigger error
$username = "xxx";
$password = "xxx";
$host = "xxxx";
$dbname = "xxx";
$db = new mysqli($host, $username, $password,$dbname);
if ($db->connect_error) {
trigger_error($db->connect_error,E_USER_WARNING);
}
上述編輯樣品工作正常,但,這是最好的做法。請指教。
你可以閱讀這個quetion,我希望它能幫助你。 http://stackoverflow.com/questions/1241728/can-i-try-catch-a-warning – user39291