CodeIgniter Forums
Global Exception Handling - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Global Exception Handling (/showthread.php?tid=26479)



Global Exception Handling - El Forum - 01-14-2010

[eluser]Treeda[/eluser]
Hi guys,

i just found out that exceptions are not get globally handled.
well for a live application i would love to catch each uncatched exception and forward me as email or write them to a log

i did the following modification to the codeigniter main file.

This change allows you to add a function named "_unhandled" to your controller class.
This methid is called always if an unhandled exception occurs in your controller and you can do what you want.

I just want to know what do you think about this?

Code:
Original Code
        // Call the requested method.
        // Any URI segments present (besides the class/function) will be passed to the method for convenience
        call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));


Code:
Modified Code as a suggestion
        // Call the requested method.
        // Any URI segments present (besides the class/function) will be passed to the method for convenience
        try{
            call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));
        }
        catch( Exception $e ){
            if ( method_exists(&$CI,"_unhandled") )
                call_user_func_array(array(&$CI, "_unhandled"), $e);
        }

in your controllers you can add the exception handler like this

Code:
public function _unhandled( $e){
        
        print "oops";
        
    }



Global Exception Handling - El Forum - 01-16-2010

[eluser]m4rw3r[/eluser]
I would recommend putting the try catch block also around the controller instantiation, as then you can also throw exceptions from eg an auth lib. Finally, if the method_exists() fails, I would have called show_error().

Also, I suggest moving this to the Feature Requests section. But I don't think it will be supported until CI goes PHP 5 fully.