0

Beginner question: I have done around 30 hours trying to sort out an error handler, essential as I am not a great programmer. I am 95% sure I can’t do anything about fatal-fatal errors but I am still 5% hopeful.

My error handler was working well sending out emails and text messages when it encounters problems but then I got an empty page with just:

Fatal error: Cannot use try without catch or finally 
in /directory/ etc ...filename.php on line 999

(I had accidentally deleted the catch block.) The question: Someone somewhere mentioned htaccess 500 pages.

I did not understand what was described when I read it. I have done almost nothing with htaccess up to now.

Is there a way to trip some sort of static page? (I am 95% sure I can do nothing but I am stuck and still have a 5% hope and this is really important for me.) I am still running PHP 5.6 but do not want to upgrade to 7 yet. Catching these errors is far more important for me than the warnings, notices, deprecateds etc that I can catch.

Update

I saw that question and used some of the techniques there BUT it is 11 years old, huge, partly outdated and does NOT primarily address the problem I now want to solve.

I have no problem dealing with "fatal errors" such as calling a non existent function. My problem is about errors found when the script is parsed and are "unrecoverable". In my case a missing catch when a try is present.

The other answer, answers this in parts but not in ways that I can seem to use. I think there maybe a way of forcing a 500 error rather perversely by stopping error display which I will investigate soon/tomorrow. I would be grateful for 24 hours to check. I am quite happy for someone more knowledgeable to put up a better question/answer and useful info could be culled from that thread but, frankly it is a mess unsurprisingly after 11 years.

Answer - almost

Switch display_errors to off and you have a 500 error. Sadly I cannot get an .htaccess redirect to work (404 works fine). If you are good with .htaccess hopefully you will have some joy.

In some discussions there is talk of some 500 errors being "CORE" errors and REALLY unrecoverable even by .htaccess. My logs are very sparse and I cannot see any useful indication if this is the case for the catch when a try is present error.

(With a big thank you to @Dharman (if it works)). PS Will tidy this up when/if I get to the end of this.)

BeNice
  • 2,165
  • 2
  • 23
  • 38
  • i did not understand exactly what's your problem. handling error is already a good way to catch errors. – Giacomo M Jul 06 '19 at 15:09
  • Sorry. I am catching all sorts of errors when I test BUT if I get a fatal parse error like this one (`catch` missing) the error handler is NOT called as the compile has failed. But PHP is sending back SOME info and I wanted a way of capturing that with a client side solution. I THINK! – BeNice Jul 06 '19 at 15:20
  • Just add a catch to your `try` block: `try {...} catch (\Exception $e) {}` or if you want the exception to be thrown, then remove the `try` – HTMHell Jul 06 '19 at 15:40
  • can't you do a redirect when error occurs? I do not remember exactly what set_error_handler does. look this thread https://stackoverflow.com/questions/23970667/redirect-to-custom-error-page-if-there-is-error-in-the-page – Giacomo M Jul 06 '19 at 15:54
  • Possible duplicate of [How do I catch a PHP Fatal Error](https://stackoverflow.com/questions/277224/how-do-i-catch-a-php-fatal-error) – Dharman Jul 06 '19 at 16:41
  • *"PHP doesn't provide conventional means for catching and recovering from fatal errors. This is because processing should not typically be recovered after a fatal error. "* - from the above post. – Dharman Jul 06 '19 at 16:41
  • Dharman thanks I had looked at that thread (it is a huge thread with 100 diff ideas). The one I thought you were talking about I now see wrapped every script in an `include` so if there is a fatal it will catch it. I don't understand but the one from @hipertracker looks best but I am confused. As to "processing should not typically be recovered after a fatal error" that had me shouting at the screen. Of course NORMAL processing should halt 100%, but allow users to send v simple error messages. This post started with a philosophical discussion on compilers. – BeNice Jul 06 '19 at 17:25
  • @Dharman You might be able to help on the 500 `htaccess` or whatever I have seen mention of this a couple places. Can I force PHP (through PHP.ini or whatever) to thow out a 500 Server Error header or whatever I need it to do? Then I can write a custom page to deal with it at least as far as telling users it is down... and more when I think about it. – BeNice Jul 06 '19 at 17:45
  • HTTP 500 error is done by your server, not PHP. Your server will show 500 page if the executed script had an error. However if PHP ini settings are set to display errors in the browser PHP will return the message instead of error 500. – Dharman Jul 06 '19 at 17:48
  • I compile locally in my editor (Notepad++) to catch syntax / parse errors before I upload files to the server. Do that and parse errors won't happen. – Dave S Jul 06 '19 at 20:17
  • Dhaman - forgive the ignorance I have a simple `ErrorDocument 404 /pages/000broken.php` in .htaccess which works fine but `ErrorDocument 500 /pages/000broken.php` does not pick up the `try` with no `catch` fatal error. I am loath to post another question but have not found anything helpful yet - any ideas. – BeNice Jul 07 '19 at 20:30
  • A PHP fatal error does not trigger what you have specified via ErrorDocument 500 - that works only for errors that have occurred _before_ the web server has passed handling of the request over to your PHP script. https://webmasters.stackexchange.com/questions/115575/how-to-load-a-custom-error-page-on-php-syntax-error – misorude Jul 08 '19 at 09:23
  • Thanks for that. BUT... If I have `errors on` I get a specified error message back so PHP is doing SOMETHING. Is there a way to grab the header & divert the page?? (I have only the loosest understanding of Apache,headers and htaccess etc.) Also where on SO can I bitch and moan about how p** poor this is of PHP. I wrote a long grump about graceful degradation. The compiler got way past the error handlers before it hit the syntax problem of a try missing a catch. When I was a little boy I used to write compilers and I cannot see ANY reason that something useful could not be sent back. ATB – BeNice Jul 09 '19 at 19:56

1 Answers1

0

I don't think PHP can do anything with parse errors (or other errors during the compilation phase), but you should be able to configure your web server to display an error page of your choosing.

You don't say what web server you are using, but for example with Apache these are the Custom Error Response settings. Your errors will be HTTP 500 errors.

bdsl
  • 288
  • 2
  • 9
  • Sorry but am now virtually certain that you CANNOT get htaccess to pick up the really HARD fails. Sad but true and IMHO stupid. You CAN get things like fatal errors from call to a non existent function but the "try without catch" error I had is NOT catchable in Apache. 99% sure of that sadly. I am beyond peerplexed why PHP could not fail more gracefully but, at present, it does not. – BeNice Jul 25 '19 at 16:16