Wednesday, May 30, 2012

Re: error code from delete or beforeDelete?

My personal view ( and I have been a software engineer for 20 years ) is that it is not good practise to rely on exceptions to do error checking for you. In languages like Java, a method that throws an exception has to be declared as doing so via a 'throws' declaration on the method...

public int foo(int param) throws XYZException, ABCException, ... 

but in PHP, there is no declaration at all. You have to read the PHP-Doc in the hope that it's up to date or examine the source code to see what exceptions could be thrown, catch them and handle them.

Exceptions are really like GOTO statements and are intended for the situation where the code cannot possibly continue. The usual response to them should be to clean up as much as you can in the catch(){} block to avoid any data corruption and stop.

Using them to catch errors can have a lot of hidden traps...  for example...

MyModel::beforeDelete() {
    if( errorcondition ) {
        throw CustomException("Oooops");
    }
    parent::beforeDelete();
}

MyController::MyFunction() {
    ...
    try {
        $this->MyModel->delete($id);
    }
    catch( CustomException $ce ){
        $this->Session->setFlash('You cant do that !!!');
    }
}


This may be OK, but what if the models parent also throws an exception ?

AppModel::beforeDelete() {
    if( error condition ){
        throw CustomException('Bad News');
    }
    parent::beforeDelete();
}

MyModel::beforeDelete() {
    if( errorcondition ) {
        throw CustomException("Oooops");
    }
    parent::beforeDelete();
}

MyController::MyFunction() {
    ...
    try {
        $this->MyModel->delete($id);
    }
    catch( CustomException $ce ){
        // OK, something bad has happened but did it happen in MyModel or AppModel ?
        $this->Session->setFlash('You cant do that !!!');
    }
}

This is what I mean about being a GOTO. The throw in the beforeDelete classes will jump straight to the first thing that can catch them, in this case the controller. If the exception occurs in the AppModel here, it will jump straight to the catch block in the controller and it will be down to the catch block to try and clean up the mess that could have been left behind.

To my mind, prevention is better than cure. If something can happen that can corrupt your data, try to remove the possibility of it happening. This can be achieved by using exceptions, but you could end up with some large catch blocks in your controllers that would be better suited to Model functions.

Steve (Ratty)

On 29/05/12 20:45, bs28723 wrote:
Thanks @stork & @steve-2  for the examples - great stuff!

Can either of you, give me some best practices for exceptions processing?  Maybe it is my self-taught PHP & CakePHP experiences but I have not a done a lot with exceptions.  Is this efficient?  Any good references to do some reading on this? 

Thanks,
bill


On 5/29/2012 2:17 PM, Steve-2 [via CakePHP] wrote:
On 29/05/12 15:35, stork wrote:

> ...or even better:
>
> try {
>     if ($this->Category->delete($id)) {
>         $this->Session->setFlash('Category has been deleted');
>         $this->redirect(array('action' => 'index'));
>     } else {
>         $this->Session->setFlash('Unknown error');
>     }
> } catch (NotEmptyCategoryException $e) {
>     $this->Session->setFlash('This category is not empty');
> }
> $this->redirect($this->referer());

...or even better:

if( $this->Category->isEmpty() ) {
     if ($this->Category->delete($id)) {
         $this->Session->setFlash('Category has been deleted');
         $this->redirect(array('action' => 'index'));
     } else {
         $this->Session->setFlash('Unknown error');
     }
} else {
     $this->Session->setFlash('This category is not empty');
}
$this->redirect($this->referer());

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php



If you reply to this email, your message will be added to the discussion below:
http://cakephp.1045679.n5.nabble.com/error-code-from-delete-or-beforeDelete-tp5708340p5708378.html
To start a new topic under CakePHP, email [hidden email]
To unsubscribe from CakePHP, click here.
NAML


View this message in context: Re: error code from delete or beforeDelete?
Sent from the CakePHP mailing list archive at Nabble.com.
--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: