Simple Way To Add Global Exception Handling In CodeIgniter

I am working on a project where we needed to capture exceptions at a global level instead of doing it at every step as they were not critical, but important for us to know.

The idea was that whenever such an exception occur on production we should send an email to developers mailing list so that someone can investigate it.

As usual I did a quick google search and i found two forum posts in CodeIgniter and one on stackoverflow, but they all fall short as CodeIgniter does not set’s any default exception handlers they way it sets the native error handler.

So here is a quick tutorial on how you can do that.

First of all you need to setup a hook, so put following code in hook.php file in config folder.

$hook['pre_controller'][] = array(
                   'class'    => 'ExceptionHook',
                   'function' => 'SetExceptionHandler',
                   'filename' => 'ExceptionHook.php',
                   'filepath' => 'hooks'
                  );

Now I am using pre_controller hook as I wanted to use $CI object which is available at this stage.

Now put the code shown below in the file named ExceptionHook.php in your application’s hooks folder.

Also if you need to capture and email native PHP errors, you can do so by extending the Exceptions library as shown in the code below.

While I have used the simple PHP mail function in the example above, you can use CI’s mail library as well.

If you have any doubts feel free to ask in comments below.

9 thoughts on “Simple Way To Add Global Exception Handling In CodeIgniter

  1. Pingback: How To Catch PHP Fatal Error In CodeIgniter | am i works?

  2. It’s been a while since you posted this, but I’ve just discovered your idea and I really needed this help. I didn’t use all the details of the MY_Exceptions class. I just wanted to convert PHP errors into ErrorExceptions so that I could use try/catch blocks consistently. I come from Java/C# thinking and I don’t like old-fashioned errors, I prefer the exception approach. Your tutorial gave me the introduction to hooks that I needed to implement an error handler globally that throws an ErrorException whenever an error occurs. If I need it, I can easily add the email logging using your ideas. Thanks

  3. Hello Sir,

    I am php developer and i liked your post and it is helpful for me in my project. But I want to know that how to use this exception in CodeIgniter Controller.

    Could you provide example how to use it?

    Thanks.

  4. Hi,

    I have tried to implement hooks in which control comes to “SetExceptionHandler” function successfully but, i wont call “HandleExceptions” function.

    I have implemented ExceptionHook method.

    Please suggest something to complete it successfully.

    Thanks

  5. Hello Sir,

    By using this code i tried to send log to my mail . But it even does not enter into show_php_error method in our file. It automatically enter into CI_Exception file.

    I need log all these error into logentries.com

Comments are closed.