Z
Z
zbender2014-08-26 13:15:25
Yii
zbender, 2014-08-26 13:15:25

Yii. How to catch notice in try?

Example:

try{
           echo  $a[не_сущестует];
 } catch (Exception $ex) { }

In this case try won't work.
How to properly configure Yii so that an error is caught in such cases?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey, 2014-08-26
@zbender

notice is not an exception. You can hang your own error handler that will transform notice into exception.
php.net/manual/ru/class.errorexception.php
ideone.com/VEuyrf - example

A
Alexander Vladimirovich, 2014-08-26
@polyanin

https://github.com/DmitryKoterov/php_exceptionizer...

V
Vit, 2014-08-26
@fornit1917

Yii has nothing to do with it.
PHP does not throw an exception when accessing an array element that does not exist. At best, it will throw out NOTICE or WARNING some thread, but this is not caught in try..catch. PHP has separate functions for handling such errors: php.net/manual/ru/book.errorfunc.php
Although Yii 2, in my opinion, made it so that php errors are converted into exceptions.

E
Eldar Amantai, 2014-08-26
@steppefox

Why turn Notice into real Exception? Well, if you really need it:

try{
    if(!isset($a['some_index']){ throw new Exception('Index does not exists!'); }
 } catch (Exception $ex) { }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question