Store PHP exceptions in an array

I'm really not sure if this is the right way, because exceptions are really a fresh subject for me. Is it possible to catch a few exceptions (let the script continue to run) and then store the exceptions in an array so that you can return all the exceptions thrown?

Thus, it would be great to be able to use exceptions more than just showing an error that kills the application (script)

Thank!

+3
source share
4 answers

At first, exception handling is not as trivial as it seems, so you should spend a little time on it. :-)

, /. , .
.

, , , . , , (, ..). :
, -

, . Logging ( , Log4PHP) .

, , (script)

, . script, , "" .: -)

. PHP W3Schools. Devshed.

+11

. - , , , .

, - "", .

+6

, script, San Jacinto , , .

, ( , ):

http://php.net/manual/en/language.exceptions.php

, .

!

+2
" ". / . , , , . , .

, - :

try {
    $fh = fopen("foo.txt", 'r');
    if (!$fh) {
       throw new Exception("foo.txt not found");
    }
    # ...
} catch (Exception $e) {
    # report errors
}

, :

@errors = array();
if (! (is_file("foo.txt") && $fh = fopen("foo.txt", 'r')) ) {
    $errors []= "foo.txt not found";
}
# ...

, , Exception.

PHP Java, IMHO.

+1

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


All Articles