Home > Backend Development > PHP Tutorial > Example explanation of using try catch in laravel5

Example explanation of using try catch in laravel5

jacklove
Release: 2023-04-02 21:22:02
Original
2261 people have browsed it

This article mainly introduces the knowledge about using try catch in laravel5. Friends who need it can refer to it

Using the following code in laravel5 does not catch the exception

try{
 var_dump($val);
}catch (Exception $e){
 var_dump($e);
echo $e->getMessage();
}
Copy after login

The Laravel 5 era controller is forced to be placed under the sub-namespace, so that the Exception class under the root namespace cannot be called directly. Laravel 4 controllers can be used directly under the namespace. After PHP 5.3, all classes will be in the namespace by default. If not declared, they will be in the top-level namespace by default.

So to use try catch syntax, either use \Exception at the beginning of the code, or use catch (\Exception $e). So the correct way to use it is

try{
 var_dump($val);
}catch (\Exception $e){
 var_dump($e);<br><br>echo $e->getMessage();
<br>
}
Copy after login

ps: try catch problem in Laravel 5: Exception cannot be detected

In a recent project, I tried to use try catch and found that There has been no success

try{
 var_dump($val);
}catch (Exception $e){
 var_dump($e);
}
Copy after login

In php, this code should print the value of $e. However, this is not the case in Laravel 5. This is because Laravel 5 enforces the use of the PSR standard and the correct namespace must be used.

So to use try catch syntax, either use \Exception at the beginning of the code, or use catch (\Exception $e). So the correct way to use it is

try{
 var_dump($val);
}catch (\Exception $e){
 var_dump($e);
}
Copy after login

Summary

The above is a detailed example of laravel5 using try catch introduced by the editor. I hope it will be useful to everyone. Help, if you have any questions please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the php Chinese website!

Articles you may be interested in:

Detailed explanation of the responsive interface provided in Laravel 5.5 for responding to requests

Explanation on php operating zip to display the pictures in the compressed package without decompressing the package

Detailed explanation of the example analysis of the method of php implementing the sign-in function

The above is the detailed content of Example explanation of using try catch in laravel5. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template