3

I get an exception with errorcode 0 but I don't why? The text/message of the exception is "Call to a member function getId() on null" but the attribute "code" is 0. Why is it 0 and not a real number? All other exception has a correct errorcode.

Is it a bug or a misconfiguration in my php.ini?

Has anybody an idea?

user3388366
  • 51
  • 1
  • 7
  • Exception on what part of code? Or function? – ThoriumBR Sep 14 '17 at 18:47
  • In a class there is that error and in my own exceptionhandler I want to handle all exceptions which there thrown in my application. In the handler I use $exception->getCode() and $exception->getMessage() – user3388366 Sep 14 '17 at 18:56
  • `Call to member function foo on null` is a fatal error. See: https://stackoverflow.com/questions/277224/how-do-i-catch-a-php-fatal-error – ficuscr Sep 14 '17 at 19:00
  • Ah, so I've no chance that a fatal error has a code? or how can I check if it's a fatal error? I use in my fatalhander the function "error_get_last()" but this is always null, but in my old php version 5.6 it works fine :) – user3388366 Sep 14 '17 at 19:02
  • 1
    You can check that the object you're trying to use, isn't null before using it. If it is, you can throw an exception. – Qirel Sep 14 '17 at 19:06

1 Answers1

7

Exceptions don't always have codes. When one is not explicitly defined, the error code defaults to 0. See http://www.php.net/manual/en/exception.construct.php - an exception will default to a message of "", a code of 0, and a previous exception of NULL.

try {
    throw new Exception('Hello!');
} catch(Exception $e) {
    echo $e->getCode(); // prints 0
}

Same thing with things like PHP errors:

try {
    $foo = new stdClass;
    $foo->fooBar();
} catch(Error $e) {
    echo $e->getCode(); // prints 0 too
    echo $e->getMessage(); // prints "Call to a member function fooBar() on null"
}
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • okey, but it in my case it is a php exception, so I hoped that I get an errorCode or an ExceptionType like "E_ERROR". But nothing :) – user3388366 Sep 14 '17 at 18:53
  • @user3388366 PHP's built-in `Error` class has the identical signature. http://php.net/manual/en/class.error.php The error code for a fatal error appears to be `0`. – ceejayoz Sep 14 '17 at 18:55
  • Ah, so I've no chance that a fatal error has a code? or how can I check if it's a fatal error? I use in my fatalhander the function "error_get_last()" but this is always null, but in my old php version 5.6 it works fine :) – user3388366 Sep 14 '17 at 19:00
  • Nice. I didn't realize that was a recoverable error. – ficuscr Sep 14 '17 at 19:04
  • Yep, this one's recoverable. I misspoke - you can't catch a fatal error, but this one's non-fatal. Edited an example of it into my answer. – ceejayoz Sep 14 '17 at 19:06
  • okey but why is "error_get_last()" null in my handler? public function fatalHandler(){ $error = error_get_last(); if($error) { $this->errorHandler($error['type'],$error['message'],$error['file'],$error['line']); } } – user3388366 Sep 14 '17 at 19:16
  • Because you *handled* the error. https://stackoverflow.com/questions/40915174/error-get-last-returns-null-after-set-error-handler-php-7-0 :-) – ceejayoz Sep 14 '17 at 19:18