List of PHP exception errors

1

Where can I find a list of exceptions error codes from try catch in PHP?

    
asked by anonymous 06.11.2014 / 13:20

2 answers

0

You can get the Exception code yes, different with the @bigown mentioned above.

I'll post a simple example showing how:

<?php
    try{
        $x = rand(0,15);

        if($x > 1 AND $x <=10){
            echo "O valor está entre 1 e 10";
        } elseif ($x == 0){
            throw new Exception("O valor não está na faixa adequada. Valor gerado : $x", 999999);           
        }
    } catch(Exception $e){
        $debug = array(
            'Mensagem' => $e -> getMessage() ,
            'Código'    => $e -> getCode()
        );

        print_r($debug);
    }
?>  

Returns:  if $ x = 1 to 10: "Value is between 1 and 10"  otherwise: "The value is not in the appropriate range." Generated value: $ x ";

You can check more details on the site below:

  

php.net: link

    
06.11.2014 / 17:35
2

Exceptions were created just so you do not need a list. It is to be destructured and even decentralized. You know which exceptions might happen by looking at the specific API documentation you are using.

There is no way to provide a list. You can create a new exception. A module adds new exceptions. An update can make new exceptions available at any time. My list would be different from yours.

And another important point, it is not enough to know which exceptions can occur. If you do not know what to do with it, you should not capture it. Of course it is possible to catch any exception (capturing the Exception which is the most generic exception). But it's almost always a mistake to do so because the only thing you can do with it is to present the error in a more cute way to the user and maybe to send it to a log system. You can not treat anything more specifically in something so generic.

You're probably asking for this because you do not know how the feature works. To learn a little more about it you can read this answer . It is following all the links, it is important. It's not about PHP but the basis is the same.

Read more in the official PHP documentation . About the Exception class . A good tutorial in English.

    
07.11.2014 / 14:55