PHP 7 でのエラー

PHP 7 は大半のエラーがどのようにPHPから報告されるかを変更しています。 PHP 5 で使われていたこれまでのエラー報告メカニズムを使うかわりに、 大半のエラーを Error 例外としてスローするようになったのです。

通常の例外と同様、Error 例外も、 最初にマッチした catch ブロックで現れます。 マッチするブロックがなければ、set_exception_handler() で設定したデフォルトの例外ハンドラが呼ばれます。 デフォルトの例外ハンドラもない場合は、例外が fatal error に変換されて、 これまでのバージョンのエラーと同じように扱われます。

Error クラスは Exception を継承していないので、 キャッチし損ねた例外を catch (Exception $e) { ... } ブロックで受け止めているような PHP 5 のコードでは、 Error をキャッチできません。 catch (Error $e) { ... } ブロックを用意するか、あるいは set_exception_handler() でハンドラを設定する必要があります。

add a note add a note

User Contributed Notes 8 notes

up
81
hungry dot rahly at gmail dot com
2 years ago
You can catch both exceptions and errors by catching(Throwable)
up
33
demis dot palma at tiscali dot it
2 years ago
Throwable does not work on PHP 5.x.

To catch both exceptions and errors in PHP 5.x and 7, add a catch block for Exception AFTER catching Throwable first.
Once PHP 5.x support is no longer needed, the block catching Exception can be removed.

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
}
up
6
ryan dot jentzsch@{gmail} dot com
1 year ago
An excellent blog post on the difference between exceptions, throwables and how PHP 7 handles these can be found here: https://trowski.com/2015/06/24/throwable-exceptions-and-errors-in-php7/
up
6
lubaev dot ka at gmail dot com
1 year ago
php 7.1

try {
   // Code that may throw an Exception or ArithmeticError.
} catch (ArithmeticError | Exception $e) {
   // pass
}
up
0
wr_sorarara at yahoo dot co dot jp
4 months ago
the node said that i can get the error by catch{Error $e}block or a set_exception_handler() handler is required.
here is my code:
try{
    array_key_exists('key',null);
}
catch(\Throwable | \Error | \Exception $e){
    echo 'hello error';
}

I deliberately wrote a warning code and the output is "Warning: array_key_exists() expects parameter 2 to be array, null given in"

the result i want is "hello error" but i didn't get it.

and in the other code :
set_error_handler('show_error');

try{
    array_key_exists('key',null);
}
catch(\Throwable | \Error | \Exception $e){
    echo 'hello error';
}

function show_error(\Error $e){
    echo "show_error:"
    echo $e->getMessage();
}

the output is "hello error".

i don't know how the error can be caught  by the catch block.
up
-10
anymous
30 days ago
The man who does not drink and does not eat meat is incredible!

After reading this sutra, I'm going to quit drinking and eat vegan.

    Excerpt from the "Generous Guang Hua Yan ten Evil Products Sutra":

    Kasyapa Bodhisattva White Buddha said: The Buddha, only the Tathagata for me to explain, do not drink, not meat eaters, how many blessings?

    Buddha-Gloucester:
    If someone, like horse cattle and sheep, glass treasures ying Luo, the country city wife, holding with giving, still less than some people can break wine meat, millions not better than one.

    Replacement is a matter, if someone Bechi gold over 3,000 of the world, holding the use of alms, still less than someone can break wine meat, millions less than one.

    The replacement is a matter, if there is the ability to cast gold for hundreds of, holding the use of giving, still less than some people can break wine meat, millions less than one.

    The replacement is a matter, if someone artificial fan Hua Bao lid, all over the 3,000 world, still less than someone can break wine meat, millions.

    The replacement is a matter, if there are man-made large pagoda, eaves eaves, such as rice hemp Bamboo reed, up to Brahma, as someone can break the wine meat, millions less than the first.

    A good man, not a carnivore, an earthly Bodhisattva, is an extraordinary husband.
    Editor's note: The front content can be seen, even if a person who does not learn Buddha, can insist on eating the whole vegetarian, Ford has boundless. Let's all go vegetarian.

All eggs are not edible, there are children also

  Someone asked the people of Xuanhua: "Why can't vegetarians eat eggs?" "

    The venerable Master said, "No Man or woman (a rooster or a hen) can hatch a chicken." The former people do not understand this truth, he said no, and there is no evidence. The man who eats eggs why does he say such a theory? Is that he wants to eat eggs. Eggs, whether or not a rooster will have chicks, will not be born. "Leng Yan Jing" said: "Eggs only want to live." "Above is the words of the master Xuan Hua is absolutely wrong." After the lying of the man in Xuanhua, he burned more than 4,000 relics to show his life not to play half a sentence of Sakyamuni Buddha, the people of Xuanhua, Inguang and Guang Chin all believe that all animals ' sperm eggs cannot be eaten. I hope we know the cause and effect and don't do anything stupid. Don't eat any food that contains eggs. South No Amitabha ~!

  "The Theory of Explicit understanding" cloud: "All eggs are not edible, have a son also".
-"Big is Tibet" 31st volume of page 882.
up
-62
Egor Gubanov
1 year ago
What should I do to let the program to react to warnings? I've already tried to write try/catch construction, but it doesn't working.
up
-59
Teo
1 year ago
Maybe you want to suppress warnings in errlog? If so, then just prepend function name with @ sign.
To Top