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?

  • one
    do you have any confusion with neymspaces? use core \ exception; catch (exception \ SessionInitException $ six). maybe you need to catch (SessionInitException $ six)? - myxaxa
  • Interface implementation is transitive, i.e. inheriting from Exception you get a Throwable implementation - it is implied that you never directly implement it. On the main issue so far I can not say anything. - etki
  • @myxaxa, yes, maybe there was a mistake, made as it should be. The question is valid. - Ep1demic
  • one
    @Etki, it’s scary for me, as a beginner, to read a comment from a user who has 45% of php answers that he cannot say anything on the main issue ((evokes hopelessness and tenseness (( - Ep1demic
  • one
    We must listen to good advice and make a repeatable example. Guessing why your code is not working for you is ineffective. - Ipatiev

2 answers 2

Problems in namespaces:

First, Exception and \Exception - 2 different classes,
the first is core\exception\Exception

Secondly, use core\exception; not enough to import exceptions.

Possible: Specify the class in full:

 try {} catch (core\exception\SessionInitException $six) {} 

Import class completely:

 use core\exception\SessionInitException; try {} catch (SessionInitException $six) {} 

Import from namespace:

 use core\exception; try {} catch (exception\SessionInitException $six) {} 

Import using alias:

 use core\exception as ex; try {} catch (ex\SessionInitException $six) {} 

Behavior in different versions is identical, except for the ability to catch Throwable in php<7

    Catch an exception as \ Exception and see what you have there

      try { // do something } catch(\Exception $e) { exit(get_class($e)); }