How to Treat a Fatal Error

Asked

Viewed 3,185 times

2

How to catch a fatal error so that it does not terminate the application? This should be limited to only one part of the code (this will not pose risks to the application)?

Example: I would like all messages from fatal error generated within this block do not close the application:

if($DocInfo->http_status_code === 200){
    //Print Page Title
    $html = str_get_html($DocInfo->content);
    $title = $html->find('title');
    echo $title[0]->plaintext.'<br />';
}
  • Sorry, excuse me, don’t close the app

  • 3

    What is the fatal error you receive? Sometimes it is better to avoid the error than try to recover from it.

  • @Andréribeiro, there is no way to avoid the fatal error, it is generated when $Docinfo->content is false, (it is false because of network error among other peculiar reasons) to circumvent the fatal error in this part of the code would not influence the correct functioning of the application

  • @Richerdohenrique and the problem is that it gets fake, just check first and don’t use it. I won’t say there’s nothing that can be avoided, of course there is, but it doesn’t seem to be this case.

1 answer

2


When you want to prevent mistakes from happening you have to ask yourself a few things:

  • What kind of problem do you expect to happen? Is it possible to do a check before it happens? If you can, do it.
  • Is it possible to check if the error occurred after without major problems? Check before the result is used and generate another error.
  • If none of this is possible, what exceptions do you know can happen in this excerpt? Capture these specific exceptions.
  • Don’t know what to capture? Capture them all. This shouldn’t be done but is the last alternative.

How to avoid error (based on what was said in the question comment):

if ($DocInfo->http_status_code === 200) {
    if ($DocInfo->content) {
        $html = str_get_html($DocInfo->content);
        $title = $html->find('title');
        echo $title[0]->plaintext.'<br />';
    } else {
        echo "deu erro aqui";
        //faz alguma coisa útil
    }
}

I would put more specific exception capture examples if I had access to the class documentation being used (and the documentation is well done, which is often rare).

The last case would be:

try { 
    if ($DocInfo->http_status_code === 200) {
        //Print Page Title
        $html = str_get_html($DocInfo->content);
        $title = $html->find('title');
        echo $title[0]->plaintext.'<br />';
    }
} catch (Exception $ex) {
    echo "deu erro aqui";
    //faz alguma coisa útil
}

I put in the Github for future reference.

See more about capturing Exception.

In addition it is possible to rewrite a function to capture all errors. It is also not recommended in most cases.

register_shutdown_function("nomeDaFuncao");

Then you write whatever you want in the function nomeDaFuncao.

But there are reasons to avoid these recommended ways. Don’t go the way that seems easier because it will become a complicator.

If you have a fatal error it is because you have a programming error. Then it must be fixed. It is not to pretend that the error does not exist. It may seem that it is not programming error but it is if you can avoid it. And it seems that it can be avoided.

  • catch will capture Fatal Error?

  • try/catch will not capture the fatal error.

  • @Ricardohenrique you’re concentrating on the wrong part of the answer.

  • @Andréribeiro As far as I know a fatal error only occurs if an exception is not captured or if the exception itself generates an error. Is there any place that would indicate otherwise? I would like to know, since PHP has these strange things. As far as I know the fatal error occurs as a result of not catching the exception.

  • @mustache See this test

  • @Andréribeiro Yes, but is there any place to document this? I don’t follow results, I like to study the official functioning. There may even be a reason for this to happen and an explanation as to why it occurs. Anyway, I do not think bad, it is programming error, and programming errors should not be captured even.

  • @Bigown In this case the try/catch does not work because no exception is generated, but a E_ERROR (which is fatal and interrupts the script). Unfortunately in the PHP documentation this is not explicitly described.

  • @Until version 5, PHP had no exception handling, it was all based on errors and warnings. All legacy code is still based on this. And errors will never be captured by a catch, unless you have instructed PHP to do this. When you say that an undetected exception generates a fatal error, would you be referring to some runtime error in PHP source code, in C++? Because in PHP even this doesn’t happen, at least not by default.

  • That’s right, I completely forgot about this. But still the solution to the AP problem is another :)

Show 4 more comments

Browser other questions tagged

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