ade19
ade19

Reputation: 1200

PHP - Error Handling

foreach ($scripts as $script) {    
    $grader = Grader::getInstance();

    $grader->setApplicantId($script['applicant_id'])
        ->setHandler($x)
        ->doGrading();

}

Grader Class (Singleton class)

 public function setHandler($x)
 {
     $this->validateHandler($x);
 }

 public function validateHandler($x)
 {
     $this->handleError("Invalid Handler");

     return $this;
 }

 public function handleError($message)
 {

 }

How to i write the handleError function such that it stops the current execution meaning return $this in the validateHandler function is never reached, prints an error message to the screen, the for loop however does not stop running?

Upvotes: 4

Views: 167

Answers (1)

Sjoerd
Sjoerd

Reputation: 75568

foreach ($scripts as $script) {    
    try {
        $grader = Grader::getInstance();

        $grader->setApplicantId($script['applicant_id'])
            ->setHandler($x)
            ->doGrading();
    }
    catch ($e) {
        echo 'Grading failed for applicant '.$script['applicant_id'];
    }
}

...

public function handleError($message)
{
    throw new Exception('Unable to fruzz the bubar');
}

Upvotes: 3

Related Questions