I’m developing some lower end code in my system that uses multiple child classes of the php exception class. Essentially I have the exceptions broken up to a few categories. What I’m wanting to do is two things.
- I need all exceptions that are fired in the application to be handled in a single place.
- I need to be able to log and then handle/generate the view for the user to receive feedback on the apps. error.
What I’m wondering is should I have some sort of try/catch encapsulating the application? I don’t like that idea at all, it sounds like a very crappy implementation. I also don’t like the idea of set_exception_handler unless i can sett the function to be a method of an object. The reason for this is that if I designate a function to handle the exceptions this will be the first function in the application. Everything else is a method of an object.
Hopefully I’ve provided enough details about the scenario. I’m trying to keep this clean and follow best practices. This code will be going OSS so I don’t feel like writing it 10 times 🙂
call
set_exception_handlerearly in execution (don’t forget to account forerror_reporting()).set_exception_handlertakes as its paramter what php calls a ‘callback’. You can pass an object method like so:Wrap your dispatching code with
try/catchto catch any code that DOES throw exceptions. The catch part of your code will catch all your own codes’ exceptions, plus some php errors that didn’t generate an exception natively (egfopenor something), thanks to yourset_exception_handlercall above. The php manual states:Log errors as necessary.
Create an error page template (the ‘View’) that operates on an Exception object (the ‘Model’) and pretty prints the whole stack trace for you, in development. Create a different template that goes to production. Branch on your environment, for example: