Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter and throwing exceptions

I recently handed in a project for school which I built in CodeIgniter. I had to present it to my teacher and when asked how I handled certain errors, he told me to throw exceptions to intercept things a lot earlier in the chain of events.

I have learnt how to throw exceptions and how to use try...catch blocks to uh, catch and handle them but somehow, when I started using CodeIgniter, I forgot all about them and didn't really use exceptions anymore.

Instead, I just handled my errors 'manually', for lack of a better word: I'd use TRUEand FALSE boolean values to check if, for example, a query executed properly, and I would use the returned boolean to handle the result of the query. If TRUE, I'd go ahead and do my stuff, if FALSE I'd 'manually' throw an error message. The project was very AJAX-dependent and the error messages would pop up in quite a fancy way, dropping down from the top of the page; not sure if this is possible when I throw an exception with throw new Exception? I know that this basically stops the code from executing when the exception is thrown, so wouldn't that break things somehow?

I also seem to remember reading somewhere that throwing exceptions isn't the best practice ever but I can't find the source of this anymore and I'm not quite sure if this is the case; after all, we did learn how to use them in class and I like to believe we learn best practices there, haha.

If necessary, I could go back and try to find the piece of code where he pointed out that I should've thrown an exception. However, for now, I'm just wondering whether or not I should use exceptions in my code or handle things manually. What are the best practices regarding this?

Thanks.

like image 522
Joris Ooms Avatar asked Jun 02 '11 17:06

Joris Ooms


People also ask

Does C language support exception handling?

C does not provide direct support for error handling (also known as exception handling). By convention, the programmer is expected to prevent errors from occurring in the first place, and test return values from functions.

What is error handling in CodeIgniter?

CodeIgniter lets you build error reporting into your applications using the functions described below. In addition, it has an error logging class that permits error and debugging messages to be saved as text files. Note. By default, CodeIgniter displays all PHP errors.

How do I enable error reporting in CI?

You can configure your CI log file by going to the config. php file in your applications/config directory and set value to $config['log_threshold'] . For a live site, you'll usually only enable Errors (1) to be logged otherwise your log files will fill up very fast.


1 Answers

Just FYI, I don't use exceptions in CodeIgniter tho I'm using them a lot in Kohana, just because the framework throws them and everything is designed to work with exceptions unlike CodeIgniter. Using exceptions is a good practice providing all your classes/framework are designed to work with them.

I don't (really, DON'T) want to enter in framework comparison discussions, but I need to compare two pieces of code to clarify your question, one piece from CI2 and another from Kohana 3 (it born as a branch of CI with better object oriented implementation).

You'll see this CI2 code...

try {     $result = $this->db->insert('entries', $this->input->post());      // This is not useful.     if ( ! $result)     {         throw new Exception();     } } catch (Exception $e) {     // Do something } 

It's not very useful. Compare with this Kohana 3 code:

try {     $entry = ORM::factory('blog');     $entry->values(Request::current()->post());     $entry->save(); } catch (ORM_Validation_Exception $e) {     Session::instance()->set('form_errors', $e->errors(TRUE)); } 

You'll see this is useful, you don't throw the exception, it's thrown by the class that handles the record saving and $e->errors has all the validation errors. When everything is designed to work with exceptions, you can be sure it's a good practice and a very convenient one. But it's not the case of CI2, so maybe I should say go ahead without using exceptions.


A possible approach to exceptions in CI...

try {     $this->load->model('blog');     $this->blog->save_entry($this->input->post());   // Handle validation inside the model with the Form_validation library } catch (Validation_Exception $e)   // You throwed your custom exception with the failed validation information {     // Do something with your custom exception like the kohana example     $this->session->set('form_errors', $e->errors()); } 

I hope everything is understandable and maybe there's someone with another interesting opinion and a more efficient implementation. Bye.

like image 139
Alejandro García Iglesias Avatar answered Sep 28 '22 06:09

Alejandro García Iglesias