Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 241359
Next
Alex Hales
  • 0
Alex HalesTeacher
Asked: August 10, 20222022-08-10T05:04:13+00:00 2022-08-10T05:04:13+00:00In: Codeigniter, error-handling, exception, PHP

php – CodeIgniter and throwing exceptions

  • 0

[ad_1]

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.

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 318 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 291 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 285 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.