Home > Backend Development > PHP Tutorial > Exception Handling in Laravel

Exception Handling in Laravel

William Shakespeare
Release: 2025-03-07 01:09:08
Original
894 people have browsed it

Exception Handling in Laravel

This article discusses in-depth the critical but rarely mentioned function in the Laravel framework - exception handling. Laravel's built-in exception handler can easily and friendlyly report and render exceptions.

The first half of the article will explore the default settings of the exception handler and analyze the default Handler class in detail to understand how Laravel handles exceptions.

The second half will demonstrate how to create a custom exception handler to catch custom exceptions.

Preparation

Before going straight into the Handler class, let's first understand a few key configuration parameters related to exceptions.

Open the config/app.php file and carefully check the following code snippet:

<code>...<br>/*<br>|--------------------------------------------------------------------------<br>| 应用调试模式<br>|--------------------------------------------------------------------------<br>|<br>| 当应用程序处于调试模式时,将显示包含堆栈跟踪的详细错误消息,<br>| 这些消息与应用程序中发生的每个错误相关联。如果禁用,则显示<br>| 一个简单的通用错误页面。<br>|<br>*/<br><br>'debug' => (bool) env('APP_DEBUG', false),<br>...<br>...<br></code>
Copy after login

As the name suggests, if set to true, detailed error information and stack trace will be displayed; if set to false, only a common error page will be displayed.

Next, let's take a look at the default reporting method, which is used to log errors to a log file. At the same time, it is important to pay attention to the rendering method, and of course, you can also customize the reporting method.

As you can see, we use the following in the app/Exceptions/Handler.php file to redirect the user to the render method:

<code>/**<br> * 将异常渲染为 HTTP 响应。<br> *<br> * @param  \Illuminate\Http\Request  $request<br> * @param  \Throwable  $exception<br> * @return \Symfony\Component\HttpFoundation\Response<br> *<br> * @throws \Throwable<br> */<br>public function render($request, Throwable $exception)<br>{<br>    if ($exception instanceof \App\Exceptions\CustomException)  {<br>        return $exception->render($request);<br>    }<br><br>    return parent::render($request, $exception);<br>}<br></code>
Copy after login

As you can see, we first check the type of the exception in the render method. If the exception type is CustomException, the render method of the class is called.

How to test our CustomException class

Everything is ready now. Next, let's create a controller file in app/Http/Controllers/ExceptionController.php to test our custom exception class.

<code><?php <br>namespace App\Http\Controllers;<br><br>use App\Http\Controllers\Controller;<br><br>class ExceptionController extends Controller<br>{<br>    public function index()<br>    {<br>        // 出现错误,您想抛出 CustomException<br>        throw new \App\Exceptions\CustomException('出现错误。');<br>    }<br>}<br></code>
Copy after login

Of course, you need to add the associated route in routes/web.php as shown below:

<code>// 异常路由<br>Route::get('exception/index', 'ExceptionController@index');<br></code>
Copy after login

With this you can visit the https://www.php.cn/link/acf7e77a5936a316105ce94cee522f5d URL to see if it works as expected. It should display the errors.custom view according to our configuration.

This is how to handle custom exceptions in Laravel.

Summary

Today, we learned the exception handling function in Laravel. At the beginning of the article, we explore the basic configuration provided by Laravel to render and report exceptions. Additionally, we briefly understand the default exception handler class.

In the second half of the article, we prepared a custom exception handler class that demonstrates how to handle custom exceptions in the application.

The above is the detailed content of Exception Handling in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template