set_exception_handler function within a class for example codeigniter

PHP Add comments

Here is how to use the set_exception_handler function within a class

the defined exception_handler method must be declared as public preferably public static if using the array syntax like array(‘Abc’, ‘exception_handler’) as suggested in site.

class Abc extends CI_Controller {
    function __construct()
        set_exception_handler(array('Abc', 'exception_handler');
        //also you can use array($this, 'exception_handler')     
    function exception_handler(Exception $ex)
        echo $ex->getMessage();
    function Index()
        throw new Exception('hehe');

The exception handler must be defined before calling set_exception_handler()

so the above code sets the default exception handler if an exception is not caught within a try/catch block. Execution will stop after the exception_handler is called.

Leave a Reply

Entries RSS