Detailed explanation of the definition and usage of PHP exception handling

墨辰丷
Release: 2023-03-26 18:12:01
Original
1494 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:

<?php
//php5提供了基本的异常处理类,可直接使用 ,不需要自己再定义
// class Exception
// {
// protected $message = &#39;Unknown exception&#39;; // 异常信息
// protected $code = 0; // 用户自定义异常代码
// protected $file; // 发生异常的文件名
// protected $line; // 发生异常的代码行号
// function __construct($message = null, $code = 0);
// final function getMessage(); // 返回异常信息
// final function getCode(); // 返回异常代码
// final function getFile(); // 返回发生异常的文件名
// final function getLine(); // 返回发生异常的代码行号
// final function getTrace(); // backtrace() 数组
// final function getTraceAsString(); // 已格成化成字符串的 getTrace() 信息
// /* 可重载的方法 */
// function __toString(); // 可输出的字符串
// }
?>
Copy after login

<?php
//定义一个顶级异常处理
function my_exception($e){
  echo "我是顶级异常处理".$e->getMessage();
}
//修改默认的顶级异常处理函数(器)
set_exception_handler("my_exception");
//我们使用异常机制
try{
  addUser("shunping");
  updateUser("xiaoming1");
}
//catch用来 捕获异常, Exception 是异常类(是php定义好一个类)
catch(Exception $e){
  //返回异常信息
  echo "返回异常信息(失败信息) =".$e->getMessage()."<br/>";
  echo "返回异常代码 =".$e->getCode()."<br/>";
  echo "返回发生异常的文件名 =".$e->getFile()."<br/>";
  echo "返回发生异常的代码行号 =".$e->getLine()."<br/>";
  //echo " =".$e->getTrace()."<br/>"; // 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.)<br/>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:

<?php
    //定义了一个异常
    class MyException1 extends Exception{
    }
    class MyException2 extends Exception{
    }
    function A(){
       throw new MyException1("a");
    }
    function B(){
       throw new MyException2("b")
    }
    function C(){
       try{
           A();//抛出MyException1
           B();//抛出MyException2
       }catch(Exception1 $e1){
           $e1->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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!