作为PHP开发人员,您可以使用exceptions ,因为它们使您可以注意到出现问题或用户以异常方式(例如被零除)采取了行动。 没有例外,您的应用程序最终会出现不必要的错误,并且调试起来会更加困难。 立即停止执行并采取其他措施也很重要。
异常非常简单,它们将使您的开发进度更加轻松。 当您学习如何使用异常时,这将是开发中的常见部分。
什么是例外?
我认为Martin Fowler给出了例外的最佳定义:
异常表示某些东西超出了预期的代码行为范围。
实际上,实际上,例外是在程序执行期间发生的事件,该事件破坏了程序指令的正常流程。 当您throw
(创建异常对象并将其交给运行时系统)异常时,系统将通过搜索适当的处理程序并返回适当的消息来catch
该异常。
try {
// the code goes here
} catch (Exception $e) {
// if an exception happened in the try block above
}
我们什么时候需要使用例外?
当系统面临阻止系统接管的异常情况时,请使用异常。 我们仅在系统无法确定发生了什么情况时才使用异常。 马丁·福勒(Martin Fowler)认为,“如果失败是预期的行为,那么您不应该使用异常”。 这意味着您应该在无法确定错误的地方使用异常。 例外仅应在特殊情况下使用。
注意:异常不利于处理逻辑运算。
对于诸如验证输入之类的系统,使用异常是错误的 。 首先,将确定输入文本,在这样的应用程序中,我们应该报告错误集合,而不是单个错误。 我相信在任何可能发生验证失败的情况下,都应避免使用异常。
捕获异常非常重要,因为如果不捕获异常,系统将返回错误。 捕获操作必须尽可能接近故障点。
什么是Laravel例外?
Laravel使用异常处理程序 ,它是App\Exceptions\Handler.php
。 这个类包含两个主要方法(该renderHttpException
其用于所有的HTTP例外,如404和503S方法,是在父类中的处理程序)。 第一个是report
,它用于记录异常或将异常发送到外部服务。 这是报告方法的示例:
public function report(Exception $e)
{
if ($e instanceof CustomException) {
//
}
return parent::report($e);
}
第二个是render
。 render方法负责将给定的异常转换为应发送回浏览器的HTTP响应。 这是render方法的示例:
public function render($request, Exception $e)
{
if ($e instanceof CustomException) {
return response()->view('errors.custom', [], 500);
}
return parent::render($request, $e);
}
注意:可以使用异常处理程序的$dontReport
属性按类型忽略异常。
您可以在自己的异常中覆盖Laravel异常方法,如下所示:
public function render($request, Exception $e)
{
if (config('app.debug')) {
return parent::render($request, $e);
}
return $this->handle($request, $e);
}
当config debug为true时,这将呈现。
如何创建自己的Laravel异常?
您可能需要创建自己的异常类。 您需要从Laravel基础Exception类扩展—我创建了一个抽象类,该抽象类将充当我们自定义Exception类的基类。 在App/Exceptions/monException.php
创建一个文件:
namespace App\Exceptions;
use Exception;
abstract class monException extends Exception
{
protected $id;
protected $details;
public function __construct($message)
{
parent::__construct($message);
}
protected function create(array $args)
{
$this->id = array_shift($args);
$error = $this->errors($this->id);
$this->details = vsprintf($error['context'], $args);
return $this->details;
}
private function errors($id)
{
$data= [
'not_found' => [
'context' => 'The requested resource could not be found but may be available again in the future. Subsequent requests by the client are permissible.',
]
// ...
];
return $data[$id];
}
}
和您的异常类:
namespace App\Exceptions;
class NotFoundmonException extends monException
{
public function __construct()
{
$message = $this->create(func_get_args());
parent::__construct($message);
}
}
您可以在代码中使用上述类:
try {
throw new \App\Exceptions\NotFoundmonException('not_found');
}
catch(\App\Exceptions\NotFoundmonException $e)
{
return $e->getMessage();
}
除了Laravel核心Exception类之外,您还可以使用Assertion包。 可以将其用作第三方异常类。 if
您的代码中有代码块, if
可以用它跳过意大利面条。
要安装Assertion软件包,应运行以下命令:
composer require beberlei/assert
例如,如果您要检查用户的电子邮件地址,则可以执行以下操作:
use Assert\Assertion;
use Assert\AssertionFailedException;
//...
try {
Assertion::email($value, "The value must be valid E-mail address");
} catch(AssertionFailedException $e) {
$e->getValue(); // the value that caused the failure
$e->getConstraints(); // the additional constraints of the assertion.
}
结论
不管您选择使用哪种语言,理解异常都是非常重要的,因为它们有助于我们控制应用程序流程的执行。
此外,它们可以帮助我们记录问题发生的时间,并有助于使我们的应用程序更强大。
从本教程介绍的工作中可以看出,Laravel是一个框架,提供了出色的异常处理功能,可用于Web构建Web应用程序。
翻译自: https://code.tutsplus.com/tutorials/what-are-laravel-exceptions--cms-25816