1

The functions I have written are throwing exceptions if they can't do their job. For the productive environment I thought to redirect the exception to a nice looking error page. Therefore I'm thinking of setting the exception handler set_exception_handler on the beginning of every script. How does the error page know which error occured? I thought of putting an error code into the URL like header("Location: error.php?code=1234"). While in the development phase I just would not set the exception handler, thus every exception would be printed onto the php default error screen Uncaugt Exception: ... with all usefull informations.

I have read Exceptions in PHP - Try/Catch or set_exception_handler? but don't know how to write a front controller script and also think this is maybe to much the effort.

I'm a PHP beginner who likes to handle errors in the right way, but I'm just not sure if I'm doing it right or wrong. Do you think it's ok doing it like above described or do you have other suggestions?

Thank you!

Community
  • 1
  • 1
Christian Ammer
  • 7,464
  • 6
  • 51
  • 108

3 Answers3

3

Don't redirect. In your exception handler function just output the error page at that point (or include a PHP file which includes the error page HTML). You also want to set an appropriate status code (using the PHP header function).

Edit: Why not to redirect:

  • You want to return an appropriate HTTP status code on your error pages (usually 404, 403, 400, 500 or 503 depending on the cause), so that search engine robots know not to index the error, crawlers can identify broken pages, browsers know not to cache the page and so on. If you redirect, you are returning a 301/302 HTTP status code and not one of the error ones.
  • You want users to be able to refresh the page with the error on, in case it was a temporary glitch. If you redirect them to another URL, however many times they refresh they will always see your error page (since that's the page they're on).
Tim Fountain
  • 33,093
  • 5
  • 41
  • 69
  • Thank you for your input. Can you please tell me your experiences, what is wrong with redirecting to a error page? I found the article [Centralized Error / Exception handling in PHP](http://codeissue.com/articles/a04e000c369b101/centralized-error-exception-handling-in-php), where the autor uses the redirecting method. – Christian Ammer Jul 01 '11 at 19:18
  • I've added an explanation to my answer. – Tim Fountain Jul 01 '11 at 20:24
2

Well to tell you the truth I think it's too early for you to worry about this kind of things.

For now just keen on mastering OOP, because later on you WILL (and probably will have to) use a MVC framework, which does all the error/exception catching for you. Take a look at symfony: in development environment it shows you exception and stack trace, but in production env. it spits out a nice, customizable error page.

What i mean is: Don't reinvent the wheel, see how others solved similar problems. And preferably use their solutions (but remember to understand them as well).

Itako
  • 2,049
  • 3
  • 16
  • 19
  • Thanks for your answer! I have heard from symfony and also installed it some time ago, but never used it productively. I don't want to reinvent the wheel, but for the project I'm working on, I think it's an overkill. The main reason not using it, is that I would have to learn it first (and unfortunately don't have the time for it). – Christian Ammer Jul 01 '11 at 20:05
1

Do you think it's ok doing it like above described or do you have other suggestions?

No, I don't think this is an OK solution. You don't want to apply a golden hammer to every problem that arises. Some exceptions should be handled differently than others. Furthermore, a particular exception may need to be handled differently in one part of the code than another. I would suggest set_exception_handler acts as a last resort, handling those exceptions that for some reason were not properly caught and go forward writing try/catches to handle things more granularly.

webbiedave
  • 48,414
  • 8
  • 88
  • 101
  • I will keep this in mind. First I try to implement the whole functionality with throwing exceptions if something went wrong. Later, if the whole functionality is implemented, I will take care on the details and error conditions. Thanks for your answer! – Christian Ammer Jul 01 '11 at 20:14