Is sending a bug report from PHP Exception __destruct () a Good or Bad Idea method?

I am developing an error handling system for my application. I want to base it on exceptions, and I want to receive email notifications of all exceptions that I did not expect.

I was thinking about the class:

class My_Exception extends Exception {

    private $sendErrorReport = true;

    public function __destruct() {
        if ($this->sendErrorReport) {
              // send the error report by email
        }            
    }

    public function cancelErrorReport() {
        $this->sendErrorReport = false;
    }

} 

And I want to do something like this:

try { 
   do_something_that_can_throw_exception();
catch (My_Exception $e) {
   if ($e->getCode() == I_KNOW_WHAT_TO_DO ) {
        react_to_exception();
        $e->cancelErrorReport();   
   } else {
        show_error_message($e->getMessage());
   }
}

So basically, when an exception occurs and the system determines what to do, it does not bother me as a developer. But when there is something that the system cannot handle, then they notify me. I don't want to post code that notifies me in a catch clause, because I'm sure I will forget it somewhere ...

Can I be sure that the __destuct exception method will be called?

__destruct ?

?

+3
2

, .

- . . set_exception_handler. , (, , ), . , , .

+1

Exception ? - :

class My_Exception extends Exception {

    private $sendErrorReport = true;

    public function __destruct() {
        if ($this->sendErrorReport) {
              // send the error report by email
        }            
    }

    public function cancelErrorReport() {
        $this->sendErrorReport = false;
    }

    public function sendEmail()
    {
        mail(....., $this->getMessage());
    }

} 

- :

try { 
   do_something_that_can_throw_exception();
catch (My_Exception $e) {
   if ($e->getCode() == I_KNOW_WHAT_TO_DO ) {
        react_to_exception();
        $e->cancelErrorReport();   
   } else {
        $e->sendEmail();
   }
}
+1

Source: https://habr.com/ru/post/1792201/


All Articles