[eluser]adamp1[/eluser]
This is what I am saying is ugly. Its complex you have error handling mixed in with the normal code. You can't just read it, you have to make sure you the statements to understand where it will jump to next.
Code:
if(($myitem = $this->mymodel->get_item($id)) === FALSE)
{
if($this->upload_file->run($my_item) === FALSE)
{
log_message('error','error msg');
$this->upload_file->cleanup_tmp_files();
show_error("Can't upload file");
}
}
else
{
log_message('error','error msg');
show_error("Can't get item");
}
But with the use of exception handling.
Code:
try
{
$myitem = $this->mymodel->get_item($id);
$this->upload_file->run($my_item);
}
catch(Exception $ex)
{
$this->upload_file->cleanup_tmp_files();
log_message('error', $ex->getMessage();
show_error($ex->getMessage());
}
The second option just makes the code easy to read and also you are writing code in a more defensive way. Also we can create are own Custom Exception class which our model and library can throw instead which always logs the details. This way you never have to worry about missing logging an error, as soon as you throw the exception it gets logged.
There is a reason PHP5 have introduced exception handling and its because it is a very good way to write well written code and handle unforeseen errors in an effective manner.