There is an exception class inherited from Exception :
<?php namespace core\exception; class SessionInitException extends \Exception{ public function __construct($message, $code = 0, Exception $previous = null) { parent::__construct($message, $code, $previous); } } In some methods, this exception is thrown. (The code of methods did not add, it is not important, all is just thrown out).
But, when catching this exception, in the code - it is not processed, but is simply skipped and the program is interrupted, respectively.
use core\exception; use types\UserShoppingData; $user_shopping_data = UserShoppingData::get_instance(); try { echo json_encode($user_shopping_data->jsonSerialize()); } catch (SessionInitException $six) { $user_shopping_data->reinit_data(); echo json_encode($user_shopping_data->jsonSerialize()); } The result of catching is:
Fatal error: Uncaught core\exception\SessionInitException: Session var's 'isUserCorrect' is not set! in F:\OpenServer\domains\talas-shop.order\application\modules\types\UserShoppingData.php:94 Stack trace: #0 F:\OpenServer\domains\talas-shop.order\application\modules\types\UserShoppingData.php(179): types\UserShoppingData->get_trash_data() #1 F:\OpenServer\domains\talas-shop.order\application\processing\service\init_template_data.php(11): types\UserShoppingData->refresh_shopping_data() #2 {main} thrown in F:\OpenServer\domains\talas-shop.order\application\modules\types\UserShoppingData.php on line 94 Although there should be an exception handling.
If you catch not exception , but already throwable :
try { $user_shopping_data->refresh_shopping_data(); echo json_encode($user_shopping_data->jsonSerialize()); } catch (Throwable $t) { echo "IN THROWABLE CATCH STATEMENT\n"; $user_shopping_data->reinit_data(); echo json_encode($user_shopping_data->jsonSerialize()); } catch (SessionInitException $six) { $user_shopping_data->reinit_data(); echo json_encode($user_shopping_data->jsonSerialize()); } It is processed as it should:
IN THROWABLE CATCH STATEMENT {"favorite_count":0,"favorite_data":[],"trash_count":0,"trash_data":[],"login_state":false} Implementation of the jsonSerialize () method:
public function jsonSerialize() { return [ 'favorite_count' => $this->favorite_count, 'favorite_data' => $this->favorite_data, 'trash_count' => $this->trash_count, 'trash_data' => $this->trash_data, 'login_state' => $this->login_state ]; } Ok, write in the documentation (the quote makes this text unreadable ):
try { // Code that may throw an Exception or Error. } catch (Throwable $t) { // Executed only in PHP 7, will not match in PHP 5 } catch (Exception $e) { // Executed only in PHP 5, will not be reached in PHP 7 } And on Habré write:
User classes cannot implement Throwable.
How then to create and catch your exceptions?