laravel如何处理错误异常?
发布于 作者:苏南大叔 来源:程序如此灵动~

laravel作为国人永远也搞不明白的神奇框架,永远都是谜一样的存在。laravel的思路,是thinkphp党们永远都无法理解的。好吧,话说,laravel接管了PHP的错误输出,普通的try catch和@操作,似乎都是失效的..... 但是,有的时候,我们真的必须拦截相关错误信息。
下面给出两个方法,是苏南大叔查看源码总结的方法。
try catch
传统的trycatch,但是需要对exception对象做个处理,加个命名空间。
try{
@$notice->to("aaaaaa")->uses($msg->template_id)->andUrl("afdafd")->data($data)->send();
}
catch (\Exception $exception){
var_dump($exception->getCode());
exit();
}
注意catch里面的exception前面有个“\”(一个反斜线),可能上边转义给转没了。
render
在app/Exceptions/Handler.php
的render
方法,可能是您需要的。
下面是个范例:
public function render($request, Exception $exception)
{
if (is_a($exception, \Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class) && $request->expectsJson()) {
return response()->json(['msg'=>'NotFound']);
} else {
return parent::render($request, $exception);
}
}


