Home > Article > Backend Development > Example explanation of using try catch in laravel5
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(); }
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> }
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); }
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); }
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!
Detailed explanation of the responsive interface provided in Laravel 5.5 for responding to requests
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!