Welcome Guest, Not a member yet? Register   Sign In
Handing errors in my code using Exceptions
#9

(This post was last modified: 09-22-2015, 10:10 AM by jLinux.)

(09-22-2015, 07:59 AM)mwhitney Wrote:
(09-21-2015, 04:21 PM)jLinux Wrote: If ErrorException or Exception shouldn't be used, I can create my own (http://php.net/manual/en/language.exceptions.php#91159)

Sorry for posting this separately from the response to includebeer above, but I missed this part before I posted that response.

You can certainly use those as much as you'd like, I just think it's more useful to use the available Exceptions for their specified purpose, especially if other people ever have to read your code (and, in my case, that other person is sometimes myself, 2 years later, wondering what I was thinking when I wrote that code). Who knows, you might even want to write an exception handler someday which handles specific types of exceptions in specific ways (though this requires slightly more effort in PHP than in some other languages, since PHP allows only one catch per try).

My primary point here is that the type of the Exception is usually more important than the message encapsulated in that Exception. If I catch an InvalidArgumentException, I know that I probably passed bad data to the function/method, so I probably don't want to continue using that data the same way (if the function has multiple arguments, I may not know which argument was invalid without some additional checks, but I know where to start if I want to check). I might even decide to only catch a subset of Exceptions and throw the rest so they can be handled by higher-level code or the framework's exception handler.

I think you think that I plan on using exceptions to basically generate every error message or notification (Which if thats how I came across, then I apologize). I really only plan on using it for serious issues, such as when an error in a lib/model was encountered that should terminate the execution of the model.

Small example..
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Computer_model extends CI_Model
{
 
   public function create($name$os)
 
   {
 
       if($this->computer_exists($name))
 
       {
 
           throw new ErrorException("The computer name {$name} already exists")
 
       }
 
       
        if
( ! $this->os_exists($os))
 
       {
 
           if( ! $this->create_os($os))
 
           {
 
               // In this case, the exception should be thrown from create_os
 
               return FALSE;
 
           }
 
           else
            
{
 
               log_message('info',"The OS {$os} doesn't exist, so I created it");
 
           }
 
       }
 
       
        
// Try to create computer
 
       if( ! $this->db->insert('computers',[ /* Data... */ ]))
 
       {
 
           // If it failed, throw an exception with the error, the SQL Query error code, and use the "severity" as an error category..
 
           throw new ErrorException("Failed to create the new computer {$name}" App_lib::ERROR_CODES['sql_query'], App_lib::ERROR_CATEGORIES['computers'])
 
       }
 
       else
        
{
 
           return TRUE;
 
       }
 
   }
}

// ... Then in the controller

$name $this->input->post('name');
$os $this->input->post('os');

try {
 
   if($this->Computer_model->create($name$os))
 
       echo "Computer was created successfully";
 
   else
        echo 
"There was an error creating the computer..";
}
catch(
Exception $e) {
 
   // Something serious happened, interpret the exception
 
   App_lib::add_notification($e);



Edit: Hm, that does mean that I would have to catch the exception in the models as well, if its relying on a method that throws exceptions
Reply


Messages In This Thread
RE: Handing errors in my code using Exceptions - by jLinux - 09-22-2015, 10:03 AM



Theme © iAndrew 2016 - Forum software by © MyBB