Home > Backend Development > PHP Tutorial > How to implement exception handling class in PHP

How to implement exception handling class in PHP

墨辰丷
Release: 2023-03-27 15:40:02
Original
1613 people have browsed it

This article mainly introduces the simple exception handling class implemented by PHP, and analyzes the relevant implementation techniques of PHP's exception handling operations based on object-oriented technology based on specific examples. Friends in need can refer to the following

for details As follows:

<?php
header(&#39;content-type:text/html;charset=UTF-8&#39;);
// 创建email异常处理类
class emailException extends exception
{
}
// 创建pwd异常处理类
class pwdException extends exception
{
  public function __tostring(){
    return $this->getMessage().&#39;in file:&#39;.$this->getFile().&#39;on line:&#39;.$this->getLine();
  }
}
function reg($reginfo = null)
{
  // 依据不同错误抛出不同异常
  if (empty($reginfo) || !isset($reginfo)) {
    throw new Exception(&#39;参数非法&#39;);
  }
  if (empty($reginfo[&#39;email&#39;])) {
    throw new emailException(&#39;邮件为空&#39;);
  }
  if ($reginfo[&#39;pwd&#39;] != $reginfo[&#39;repwd&#39;]) {
    throw new pwdException(&#39;两次密码不一致!&#39;);
  }
}
// 接收不同异常,并针对性处理!
try {
  reg(array(&#39;email&#39; => &#39;1078789950@qq.com&#39;, &#39;pwd&#39; => &#39;123&#39;, &#39;repwd&#39; => &#39;1231&#39; ));
} catch (Exception $e) {
  echo $e ->getMessage();
} catch (emailException $ee) {
  echo $ee ->getMessage();
} catch (pwdException $ep) {
  echo $ep;
}
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHP Example code for strstr function to determine whether a string exists_php basics

The difference between time() and mktime() methods in php_php basics

php array declaration, traversal, array Summary of using global variables_php basics

The above is the detailed content of How to implement exception handling class in PHP. 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