1

Here's what I want to do just if a fatal error is in the file I don't want the script to halt execution

try { 
require "somethin.php"
} catch(...) {}

Would some method of file_get_contents() and eval() be a way around

8
  • Why didn't you try it first?
    – samayo
    Commented Jun 11, 2013 at 22:31
  • 3
    In fairness he probably did try it, haha did you catch that?
    – Dave Chen
    Commented Jun 11, 2013 at 22:35
  • @DaveChen You are saying "probably" because, he did not give any hints whether if he did/or not. So, who is the actaully [censored] here?hmm. Sadly, I read his disgusting comment.
    – samayo
    Commented Jun 11, 2013 at 22:38
  • For me, I did try it, puu.sh/3dF22.png and it still erred, so that leads the conclusion that he did try it before posting.
    – Dave Chen
    Commented Jun 11, 2013 at 22:39
  • @DaveChen So for every question asked in SO with the OP failing to specify whether he tried/or not. We are supposed to try it out for them by ourselves? hmm.. interesting.
    – samayo
    Commented Jun 11, 2013 at 22:44

2 Answers 2

6

Notice - this does not answer the poster's question. The ugly reality is you cannot recover from a fatal error but this works for other errors

require doesn't throw an exception but rather triggers an error by default.

To have php throw exceptions you have to register an error exception handler.

You can find an example here

But basically it

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");

With this in play it's possible to catch and deal with the error as you see fit

16
  • 1
    @DaveChen That's one approach ... I like this better because it's only one hit on the filesystem
    – Orangepill
    Commented Jun 11, 2013 at 22:35
  • 1
    It's also worth noting that this will cause almost all php errors to throw exceptions (a good thing in my opinion) except for parse time errors
    – Orangepill
    Commented Jun 11, 2013 at 22:40
  • 1
    like syntax errors? Having all PHP errors/warnings throw exceptions, oh gosh it's like Java all over again.
    – Dave Chen
    Commented Jun 11, 2013 at 22:41
  • 1
    This doesn't catch fatal errors (which really shouldn't be catched in the first place).
    – PeeHaa
    Commented Jun 11, 2013 at 22:45
  • 2
    @PeeHaa埽 Follow the link, read the docs. This deals with fatal errors
    – Orangepill
    Commented Jun 11, 2013 at 22:51
5

No there is really no sane way to catch (uncatchable ;-) ) fatal errors in PHP. So there is also no sane way to recover from this. Which makes sense because it is a fatal error.

Basically what PHP is saying is: this is something you cannot / should not try to recover from.

Note that also @Orangepill's solution won't work for fatal errors: http://codepad.org/4nzzYQSG

Now if you would want to do something when a fatal error happens (e.g. send an email to you to inform you of this) you may want to look into: https://stackoverflow.com/a/2146171/508666

6
  • +1 My solution below while useful won't even throw something given the example above.
    – Orangepill
    Commented Jun 11, 2013 at 23:05
  • +1 for making me proud. (even though, I don't know what you guys are talking about :P )
    – samayo
    Commented Jun 11, 2013 at 23:06
  • So what is the right thing to do ... nix my answer or just but a banner across the top stating it doesn't answer the question?
    – Orangepill
    Commented Jun 11, 2013 at 23:08
  • @Orangepill Close it, as not a real question
    – samayo
    Commented Jun 11, 2013 at 23:08
  • nah the question is good... there just is not an answer the the op wants.
    – Orangepill
    Commented Jun 11, 2013 at 23:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.