Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exceptions in PHP - Try/Catch or set_exception_handler?

Tags:

exception

php

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.

  1. I need all exceptions that are fired in the application to be handled in a single place.
  2. 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 :)

like image 826
Syntax Avatar asked Feb 17 '09 14:02

Syntax


People also ask

Does PHP have try catch?

The primary method of handling exceptions in PHP is the try-catch. In a nutshell, the try-catch is a code block that can be used to deal with thrown exceptions without interrupting program execution. In other words, you can "try" to execute a block of code, and "catch" any PHP exceptions that are thrown.

What are exceptions in PHP?

An exception is an object that describes an error or unexpected behaviour of a PHP script. Exceptions are thrown by many PHP functions and classes. User defined functions and classes can also throw exceptions. Exceptions are a good way to stop a function when it comes across data that it cannot use.

What are exceptions caught and handled by PHP?

With PHP 5 came a new object oriented way of dealing with errors. Exception handling is used to change the normal flow of the code execution if a specified error (exceptional) condition occurs. This condition is called an exception.

Can PHP try have multiple catches?

PHP allows a series of catch blocks following a try block to handle different exception cases. Various catch blocks may be employed to handle predefined exceptions and errors as well as user defined exceptions.


3 Answers

  1. Run your web requests through a Front Controller script
  2. call set_exception_handler early in execution (don't forget to account for error_reporting()). set_exception_handler takes as its paramter what php calls a "callback". You can pass an object method like so:

    // $object->methodName() will be called on errors
    set_exception_handler(array($object, 'methodName'));
    
  3. Wrap your dispatching code with try/catch to 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 (eg fopen or something), thanks to your set_exception_handler call above. The php manual states:

    The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.

  4. Log errors as necessary.

  5. 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:

    catch(Exception $e) {
        // log error as necessary here.
        if("in developement") {
            // $e would be available to your template at this point
            include "errortemplates/dev.php";
        } else {
            include "errortemplates/prod.php";
        }
    }
    
like image 166
Crescent Fresh Avatar answered Oct 19 '22 19:10

Crescent Fresh


There's more specific information about PHP's "callbacks" here. To use a static method, the callback is something like

<?php
set_exception_handler(array('MyClass','staticMethod'));
?>

To use a method from an instantiated object, it's:

<?php
set_exception_handler(array($myObject, 'objectMethod'));
?>

And to use a global function, it's just:

<?php
set_exception_handler('my_global_function');
?>
like image 31
James Socol Avatar answered Oct 19 '22 20:10

James Socol


From the way it sounds, you will be using set_exception_handler. This will guarantee that all exceptions are handled in the exact same way. There are places to use the try/catch blocks in your application, say if you want to check for a single exception that does not necessarily need to be caught in the same way.

As far as setting set_exception_handler, I'm not sure if you can set the function to be a method of an object unless it is a static method. It appears that is the case. There is more information at http://us2.php.net/set_exception_handler

like image 33
scheibk Avatar answered Oct 19 '22 19:10

scheibk