Detailed explanation of the definition and usage of PHP exception handling

墨辰丷
Release: 2023-03-26 18:12:01
Original
1485 people have browsed it

This article mainly introduces the definition and usage of PHP exception handling. It analyzes the definition, usage and related precautions of PHP exception handling in more detail. Friends in need can refer to it

The details are as follows:

Copy after login

getMessage(); } //修改默认的顶级异常处理函数(器) set_exception_handler("my_exception"); //我们使用异常机制 try{ addUser("shunping"); updateUser("xiaoming1"); } //catch用来 捕获异常, Exception 是异常类(是php定义好一个类) catch(Exception $e){ //返回异常信息 echo "返回异常信息(失败信息) =".$e->getMessage()."
"; echo "返回异常代码 =".$e->getCode()."
"; echo "返回发生异常的文件名 =".$e->getFile()."
"; echo "返回发生异常的代码行号 =".$e->getLine()."
"; //echo " =".$e->getTrace()."
"; // backtrace() 数组 //可以继续抛出,这时将会启动php默认的异常处理器来处理 //你也可以自己定义一个顶级异常处理器,对其进行处理 throw $e; } function addUser($username){ if($username=="shunping"){ //添加ok echo "ok1"; }else{ //添加error //抛出异常. throw new Exception("添加失败"); } } function updateUser($username){ if($username=="xiaoming"){ //正常修改 echo "ok2"; }else{ //平抛出异常 throw new Exception("修改失败"); } } ?>
Copy after login

## Notes on using exceptions:

From the above case, we can see that using

try{ //代码 }catch(Exception $e){ //对异常处理 }
Copy after login

can more effectively control errors. Therefore, it is widely used in development.

1. When an exception is caught, the subsequent code in the try{} block will not continue to execute.

2. If an exception occurs but you do not catch it, an
Uncaught will be prompted. Exception.(System.)
3. When catching an exception, you can handle it or not. If you don’t handle it, you can

throw new Exception("information");

4. You can customize the exception class

class MyException extends Exception { }
Copy after login

5. Use multiple catch code blocks to catch different types of exceptions

try{ //代码.... //第一句话 //第二句话 }catch(PDOException $e){ // }catch(Exception $e){ | }
Copy after login

6. You can also simply use Exception capture

Code description:

getMessage(); }catch(Exception2 $e2){ $e2->getMesage(); } } ?>
Copy after login

Exception rules

Code that requires exception handling should be placed in a try code block to catch potential exceptions.

Each try or throw code block must have at least one corresponding catch code block.
Use multiple catch code blocks to catch different types of exceptions.
Exceptions can be thrown (thrown again) in the catch code block within the try code block.
In short: if an exception is thrown, you must catch it. Or use a top-level exception handler.
If it is not thrown, even if there is an exception, it will not be caught or handled

Related recommendations:

PHP exception handlingSharing error handling methods

What isPHP exception handling

## Customization of PHP exception handler

The above is the detailed content of Detailed explanation of the definition and usage of PHP exception handling. For more information, please follow other related articles on the PHP Chinese website!

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