Take the 2-minute tour ×

This question already has an answer here:

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

share|improve this question

marked as duplicate by samaYo, Ja͢ck, DaveRandom, Achrome, Graviton Jun 15 '13 at 5:02


Why didn't you try it first? –  samaYo Jun 11 '13 at 22:31
In fairness he probably did try it, haha did you catch that? –  Dave Chen Jun 11 '13 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 Jun 11 '13 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 Jun 11 '13 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 Jun 11 '13 at 22:44

2 Answers 2

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: http://stackoverflow.com/a/2146171/508666

share|improve this answer
+1 My solution below while useful won't even throw something given the example above. –  Orangepill Jun 11 '13 at 23:05
+1 for making me proud. (even though, I don't know what you guys are talking about :P ) –  samaYo Jun 11 '13 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 Jun 11 '13 at 23:08
@Orangepill Close it, as not a real question –  samaYo Jun 11 '13 at 23:08
nah the question is good... there just is not an answer the the op wants. –  Orangepill Jun 11 '13 at 23:11

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


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

share|improve this answer
@DaveChen That's one approach ... I like this better because it's only one hit on the filesystem –  Orangepill Jun 11 '13 at 22:35
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 Jun 11 '13 at 22:40
like syntax errors? Having all PHP errors/warnings throw exceptions, oh gosh it's like Java all over again. –  Dave Chen Jun 11 '13 at 22:41
This doesn't catch fatal errors (which really shouldn't be catched in the first place). –  PeeHaa Jun 11 '13 at 22:45
@PeeHaa埽 Follow the link, read the docs. This deals with fatal errors –  Orangepill Jun 11 '13 at 22:51
