Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter 2.0 & Exceptions
#1

[eluser]adamp1[/eluser]
I know the information on CI 2.0 is very thin on the ground at the moment. But was wondering if anyone had heard anything about it using the PHP 5 exceptions to throw error details rather than return TRUE/FALSE from a method as it currently does?
#2

[eluser]richthegeek[/eluser]
I really hope it *doesn't*.

I like being able to test results with a simple "if( $result )". Exceptions are a really poor way of doing things for PHP, and if you like them go use Java ;D
#3

[eluser]adamp1[/eluser]
But testing every single method for a bool result means you can get massive nesting of if statements. Also what happens if the method fails before it gets to return FALSE at the end? You are then left with an unhandled error.
#4

[eluser]richthegeek[/eluser]
If this is occuring, your are probably doing your app wrong.

Secondly, I tend to do something like:
Code:
if( ! $result ) show_error( "some error" );

That outputs the error and the whole script dies.

The objective is to get to a stage where you never see that function actually get called.
#5

[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.
#6

[eluser]richthegeek[/eluser]
I find that less readable to be honest.

I can only assume you learned code in a very formal way on a CompSci course and haven't got used to the idea that it's total BS for scripting languages.
#7

[eluser]adamp1[/eluser]
As a matter of fact I did, I am a software engineer by profession. But I see no reason why programming techniques cannot be used in scripting languages. Scripting languages are good because they are weekly typed and allow people to get to know the basics, but at the same time they lead to issues with the code. Maintainable, readability (in my opinion) and safeness can suffer.

But with PHP5 and PHP6 on the way techniques which have been used in programming for years are coming to the developers hands. We already enjoy things like classes and objects so why not other things. I don't want to get into a long debate on which is better, you have your opinion and I have mine.

So from your initial response I take it you don't know if CI 2.0 shall use Exceptions or not.
#8

[eluser]Sbioko[/eluser]
I think there are still TRUE/FALSE. I think exceptions will be in CI 2.1. Read EllisLab's blog post about CI 2.0 and you'll get the answer on your question.
#9

[eluser]adamp1[/eluser]
Wheres the blog I can't find it anywhere. I can find the CI news page but thats all.
#10

[eluser]Sbioko[/eluser]
Yeah, I mean CI news.




Theme © iAndrew 2016 - Forum software by © MyBB