error handling - Exceptions in PHP - set_exception_handler output -


i'm starting use set_exception_handler now. first place tested try/catch block pdo layer.

i forced exception incorrect database credentials. (this before applied

<?php    function log_exception($exception){     print_r($exception);     }  set_exception_handler("log_exception");  try {      $dbh = new pdo();     $dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception);      echo "connected!! \n"; } catch (pdoexception $e) {      print_r($e); // let's see looks     throw new exception($e);  } ?> 

you'll see print_r inside catch, in log_exception function.

this gets displayed print_r($e):

pdoexception object (     [message:protected] => sqlstate[28000] [1045] access denied user 'localhost'@'127.0.0.1' (using password: yes)     [string:exception:private] =>     [code:protected] => 1045     [file:protected] => /var/www/html/test.php     [line:protected] => 35     [trace:exception:private] => array         (             [0] => array                 (                     [file] => /var/www/html/test.php                     [line] => 35                     [function] => __construct                     [class] => pdo                     [type] => ->                     [args] => array                         (                             [0] => mysql:host=127.0.0.1;dbname=db_tests;charset=utf8                             [1] => test                             [2] => test                         )                  )          )      [previous:exception:private] =>     [errorinfo] => ) 

and gets displayed print_r($exception) inside log_error() function:

exception object (     [message:protected] => exception 'pdoexception' message 'sqlstate[28000] [1045] access denied user 'localhost'@'127.0.0.1' (using password: yes)' in /var/www/html/test.php:34 stack trace: #0 /var/www/html/test.php(34): pdo->__construct('mysql:host=127.0...', 'test', 'test') #1 {main}     [string:exception:private] =>     [code:protected] => 0     [file:protected] => /var/www/html/test.php     [line:protected] => 34     [trace:exception:private] => array         (         )      [previous:exception:private] => ) 

what different? assuming whatever exception object passed log_error() function going same got generated within catch.

am missing something?

...

you should throwing same exception if want forward it, not new one:

throw $e; 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -