Ian
Ian

Reputation: 379

set_exception_handler() expects the argument (exception_handler) to be a valid callback

I have the following code:

namespace Google\Ads\GoogleAds\Examples\BasicOperations;

set_exception_handler('exception_handler');


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

And get the following error (PHP 7.3):

Warning: set_exception_handler() expects the argument (exception_handler) to be a valid callback

It seems like the namespace has to be used to reference the exception handler function - such as set_error_handler("MyNamespace\my_error_handler"); - but I have not found an example that works correctly.

Upvotes: 1

Views: 1455

Answers (3)

Steve Moretz
Steve Moretz

Reputation: 3128

You have to use the namespace in there too,that's why it was not working.

set_exception_handler('Google\Ads\GoogleAds\Examples\BasicOperations\exception_handler');

instead of :

set_exception_handler('exception_handler');

Upvotes: 1

Zadat Olayinka
Zadat Olayinka

Reputation: 471

Alternatively, this works too

set_exception_handler(array($this,'exception_handler'));

Upvotes: 1

fonini
fonini

Reputation: 3341

This works:

<?php

namespace Google\Ads\GoogleAds\Examples\BasicOperations;

set_exception_handler('Google\Ads\GoogleAds\Examples\BasicOperations\exception_handler');

throw new \Exception('test');

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

https://3v4l.org/bbmL1

Upvotes: 0

Related Questions