List of Exception PHP errors

Asked

Viewed 455 times

1

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

  • 7

    Exceptions have been created just to not need it. There is no way. 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. You are probably asking this because you do not know how the resource works. To learn more you can read http://answall.com/questions/30124/h%C3%A1-some-inconvenient-always-catch-Exception-e-n%C3%A3o-algo-mais-espec%C3%Adfico/30168#30168. Follow all links, it’s important. It’s not about PHP but the basis is the same.

2 answers

4

Exceptions were created just to not need a list. It is to be unstructured and even decentralized. You know what exceptions can happen by studying 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 what exceptions may occur. If you do not know what to do with it, you should not capture it. Of course it is possible to capture any exception (by capturing the Exception which is the most general exception). But it is almost always a mistake to do so because the only thing you can do with it for sure is to present the error in a cuter way to the user and maybe send to a system of log. You can’t treat anything more specifically on something so generic.

You are probably asking this because you do not know how the resource works. To learn a little more about the subject you can read that answer. Follow all links, it’s important. It’s not about PHP but the basis is the same.

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

0


You can get the code from Exception yes, different with @Maniero said 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);
    }
?>  

Will return:

 se $x = 1 a 10 : "O Valor está entre 1 e 10"
 caso contrário : "O valor não está na faixa adequada. Valor gerado : $x";

You can check more details in php.net.

  • 2

    I understand that the code mentioned can be used to get the message from Exception, (which may be a good solution to the author’s problem). Just one remark: the comment you referred to is about there being a list of these codes, and in fact the comment proceeds.

  • There really is no list. You will have to go by 'trial-error-documentation' as it appears as it was said.

  • @Douglasdreer didn’t come to think this way, but that’s exactly what I wanted, to know what each code represents error, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.