Home > Backend Development > PHP Tutorial > Detailed explanation of laravel5 examples using try catch_php examples

Detailed explanation of laravel5 examples using try catch_php examples

韦小宝
Release: 2023-03-17 18:24:02
Original
1552 people have browsed it

This article mainly introduces the relevant knowledge of laravel5 . Friends who are interested in laravel5 and want to learn laravel5 can refer to it. Next article!

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


Laravel 5 eraController was 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: Unable to detect Exception

In a recent project, Trying to use try catch, I found that it has never been successful


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 explanation of the examples of using try catch in laravel5 introduced by the editor. , I hope it will be helpful to everyone. If you have any questions, you can communicate in the community Q&A on this site!

Related recommendations:

How to catch exceptions in try catch of TP3.2

What problem does try catch in php solve?

php Try Catch exception testing_PHP tutorial

The above is the detailed content of Detailed explanation of laravel5 examples using try catch_php examples. 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