XaBerr
XaBerr

Reputation: 360

PHP get last error

I am trying to recover the last (fatal) error message through these functions:

$last_error = error_get_last();
echo $last_error['message'];

Using the exception:

throw new Exception("Error n.1");

I would expect to get this string "Error n.1", but I get something like this (which depends on the PHP version):

Uncaught Exception: Error n.1 in C:\wamp\www\JUICE\projects\JATE\dist\jate\functions\errorHandler.php:66 Stack trace: #0 C:\wamp\www\JUICE\projects\JATE\dist\jate\functions\requirer.php(37): require_once() #1 C:\wamp\www\JUICE\projects\JATE\dist\jate\functions\requirer.php(5): jRequire('C:\\wamp\\www\\JUI...', false, 0) #2 C:\wamp\www\JUICE\projects\JATE\dist\jate\functions\requirer.php(15): requireComponent('C:\\wamp\\www\\JUI...', false) #3 C:\wamp\www\JUICE\projects\JATE\dist\jate\coreEngine.php(10): requireComponents('functions') #4 C:\wamp\www\JUICE\projects\JATE\examples\01essential\jate.php(15): require_once('C:\\wamp\\www\\JUI...') #5 C:\wamp\www\JUICE\projects\JATE\examples\01essential\index.php(2): require_once('C:\\wamp\\www\\JUI...') #6 {main} thrown

How can I get the error string and not all the things added by the system?

Upvotes: 0

Views: 1070

Answers (1)

Barmar
Barmar

Reputation: 781731

Your expectation is wrong. Throwing an exception doesn't cause an error by itself, so that's not what is shown in $last_error. Failing to catch an exception causes an error, but the message for that error is Uncaught Exception followed by the details of the exception, not just the exception string.

Since the specific format of that message is version-dependent, the best you can do is search it for the exception string in it:

if (preg_match('/Error n\.1/', $last_error['message']) {
    echo "Error n.1";
}

Upvotes: 1

Related Questions