Exception message lost when set_exception_handler used in a custom exception class

#1

Hi there, I have a pretty obscure issue (to me) trying to define a custom error handling class which extends php native Exception class.

From the php documentation, here’s how a simple global exception handler is implemented:

<?php

function exception_handler($exception) {
	var_dump($exception);
	echo "Uncaught exception: " , $exception->getMessage(), "\n";
}

set_exception_handler('exception_handler');

throw new Exception('This is the message!');
echo "Not Executed\n";

Below is the result from the var_dump of the previous code:

C:\WinNMP\WWW\dispatcher\webroot\api\test.php:4:
object(Exception)[1]
  protected 'message' => string 'This is the message!' (length=20)
  private 'string' => string '' (length=0)
  protected 'code' => int 0
  protected 'file' => string 'C:\WinNMP\WWW\dispatcher\webroot\api\test.php' (length=45)
  protected 'line' => int 10
  private 'trace' => 
    array (size=0)
      empty
  private 'previous' => null
  public 'xdebug_message' => string 'removed since too large...'
  
Uncaught exception: Uncaught Exception

As you can tell the protected ‘message’ variable contains the expected message from the exception.

Now I’d like to implement this using a custom ExceptionHandler class extending php Exception class like below:

<?php

namespace NLE\Exceptions;

use Exception;

class ExceptionHandler extends Exception {
	
	public function __construct() {
		set_exception_handler( array($this, 'exception_handler') );
    }

    public function exception_handler( $ex ) {
		var_dump( $ex );
		echo "Exception:" . $ex->getMessage();
	}
	
}

<?php                                                                                                     
                                                                                                          
include_once filter_input(INPUT_SERVER, 'DOCUMENT_ROOT', FILTER_SANITIZE_URL) . '/../vendor/autoload.php';
                                                                                                          
use NLE\Exceptions\ExceptionHandler;                                                                      
                                                                                                          
$test = new Test();                                                                                       
                                                                                                          
class Test {                                                                                              
                                                                                                          
	function __construct() {                                                                              
		echo 'Test<br>';                                                                                  
		throw new ExceptionHandler( "It works ?? \n");                                                    
	}                                                                                                     
                                                                                                          
}

C:\WinNMP\WWW\dispatcher\class\Exceptions\ExceptionHandler.php:19:
object(NLE\Exceptions\ExceptionHandler)[2]
  protected 'message' => string '' (length=0)
  private 'string' (Exception) => string '' (length=0)
  protected 'code' => int 0
  protected 'file' => string 'C:\WinNMP\WWW\dispatcher\webroot\api\exception.php' (length=50)
  protected 'line' => int 17
  private 'trace' (Exception) => 
    array (size=1)
      0 => 
        array (size=6)
          'file' => string 'C:\WinNMP\WWW\dispatcher\webroot\api\exception.php' (length=50)
          'line' => int 10
          'function' => string '__construct' (length=11)
          'class' => string 'Test' (length=4)
          'type' => string '->' (length=2)
          'args' => 
            array (size=0)
              empty
  private 'previous' (Exception) => null
  public 'xdebug_message' => string 'removed since too large...'

Exception:

This time though, the output from the var_dump above shows an empty string for protected ‘message’.

Any help would be appreciated.