0

I'm trying to customise the exception error with my custom error message as per below:

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; 
    } 

My expectation was, I managed to echo the custom message instead the default message and I managed to write in the log file. But, I'm still getting the default error message instead the custom one as per image below. enter image description here

Any idea where I did the mistake. Please advice.

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);
}

The above edited sample working fine but, is this the best practice. Please advice.

chinna_82
  • 6,353
  • 17
  • 79
  • 134
  • You can read this quetion , I hope it can help you. http://stackoverflow.com/questions/1241728/can-i-try-catch-a-warning – jamesxu-e.g. Sep 23 '15 at 07:54

1 Answers1

0

It isn't exception. You try to catch an error. You need to set your error handler function to convert error/warnings etc to throw exceptions. In PHP7 it will be changed and you will be able to catch Error or \Throwable interface

To change errors to exception you will need to use set_error_handler() function. You may also find this interesting

Edit:

<?php

error_reporting(E_ALL);
//error handler function
function customError ($errno, $errstr) {
    if (error_reporting() == 0) {
        return;
    }
    $timelog = date("d-m-Y h:i:s", time());
    throw new Exception($timelog . ':' . $errstr, $errno);
}

//set error handler
set_error_handler("customError", E_ALL & ~E_NOTICE);
//trigger error
try {
   $error = 5/0;
} catch(Exception $e) {
    echo "Error caught : " . $e->getMessage();
}

?>

This is still very basic example. It would be better if you create your own exception class with better handling error priorities.

Robert
  • 19,800
  • 5
  • 55
  • 85
  • I've edited the answer with your suggestion. Is that the best practise? – chinna_82 Sep 23 '15 at 08:31
  • No it is not. This function should throw exception if you want that way and you should handle exception in your app. Also notice that sometimes it is senseless to throw exception for example for notice – Robert Sep 23 '15 at 08:37
  • Is there any possibilities that you change my code to your suggestion. I can get more clear picture on it. – chinna_82 Sep 23 '15 at 08:55