Yii2 try catch exception的正确姿势

技术分享 · Fecmall · 于 5年前 发布 · 2378 次阅读
try {
	$mailer->compose()
		->setFrom($setFrom)
		->setTo($to)
		->setSubject($subject)
		->setHtmlBody($htmlBody)
		->send();
	return true;
} catch (Exception $e) {
	var_dump($e);
	Yii::$service->helper->errors->add('email send from is empty');
	return false;
}

通过上面的代码是无法获取异常的,需要将 Exception $e 改成 \Exception $e 才行

改后的代码:

try {
	$mailer->compose()
		->setFrom($setFrom)
		->setTo($to)
		->setSubject($subject)
		->setHtmlBody($htmlBody)
		->send();
	return true;
} catch (\Exception $e) {
	var_dump($e);
	Yii::$service->helper->errors->add('email send from is empty');
	return false;
}
共收到 2 条回复
successgo#15年前 0 个赞

对的,现有 fecshop 源码有很多处是没有使用 \Exception 写法的,并且会在 IDE 中提示:class not found

Fecmall#25年前 0 个赞

@successgdc #1楼

已经修改了,我的github账户出问题了,提交不了代码,给github发送邮件了,看看怎么处理

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics