How to throw an exception from a CodeIgniter library How to throw an exception from a CodeIgniter library codeigniter codeigniter

How to throw an exception from a CodeIgniter library


If the goal is just a pretty error message instead of a fatal error, it's pretty simple really:

$this->func = is_callable(array($this->db, '_compile_select')) ? '_compile_select' : 'get_compiled_select';if ( ! is_callable(array($this, $this->func))){    show_error("You can't use this library, you're missing a function");}

Basically, just don't assume get_compiled_select is callable in your conditional - check first.

can I call show_error from a library?

Yes, this is one of the functions defined in core/Common.php, you can use it anywhere in your CI app.

Of course, technically this is not throwing an exception, but it's the convention for Codeigniter. This can be problematic if you want to catch errors and try something else:

try {    $this->load->library('might_not_exist', 'alias');} catch (Exception $e) {    $this->load->library('definitely_exists', 'alias');}

The above won't work, as show_error() will get called by the loader and exit the program before your catch block is executed.


I am not sure but see this URL i think it is very help full to you.

http://codeigniter.com/forums/viewthread/67096/

see also

http://phpcodeignitor.blogspot.in/2011/07/php-exception.html

or try this

MY_Exceptions.php and put it in /applications/libraries/

 <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');class MY_Exceptions extends CI_Exceptions{    function MY_Exceptions(){        parent::CI_Exceptions();    }    function show_404($page = '')    {    echo 'test';    }    function show_error($heading, $message, $template = 'error_general')    {echo 'test';    }    function show_php_error($severity, $message, $filepath, $line)    {         echo 'test';         }}?>

functions in MY_Exceptions do not seem to be overridden at all. functions in Exceptions are the one which are run