gpt4 book ai didi

PHP - 错误处理

转载 作者:可可西里 更新时间:2023-10-31 23:23:26 25 4
gpt4 key购买 nike

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

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

}

Grader类(单例类)

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

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

return $this;
}

public function handleError($message)
{

}

我如何编写 handleError 函数,使其停止当前执行,这意味着永远不会到达 validateHandler 函数中的 return $this,将错误消息打印到屏幕,但是 for 循环不会停止运行?

最佳答案

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');
}

关于PHP - 错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11244229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com