0

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?

10
  • do some string-fu? get the substring after Uncaught exception (and before "in") Commented Dec 22, 2017 at 22:20
  • Why aren't you using try/catch? Then you can use $error->getMessage(). Commented Dec 22, 2017 at 22:49
  • @dGRAMOP I can not because the string depends on the version of php the string. Commented Dec 23, 2017 at 8:10
  • @Barmar I'm throwing the exceptions, I do not want to capture them! Commented Dec 23, 2017 at 8:11
  • 1
    Nope. There's no more structure to $last_error than that. The error message is just one string, it's not layered in any way just because the error refers to an exception. Commented Dec 23, 2017 at 8:34

1 Answer 1

1

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";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.