SoFunction
Updated on 2025-04-08

Example of simple exception handling class implemented by PHP

This article describes the simple exception handling class implemented by PHP. Share it for your reference, as follows:

<?php
header('content-type:text/html;charset=UTF-8');
// Create an email exception handling classclass emailException extends exception
{
}
// Create pwd exception handling classclass pwdException extends exception
{
  public function __tostring(){
    return $this->getMessage().'in file:'.$this->getFile().'on line:'.$this->getLine();
  }
}
function reg($reginfo = null)
{
  // Throw different exceptions according to different errors  if (empty($reginfo) || !isset($reginfo)) {
    throw new Exception('The parameter is illegal');
  }
  if (empty($reginfo['email'])) {
    throw new emailException('The email is empty');
  }
  if ($reginfo['pwd'] != $reginfo['repwd']) {
    throw new pwdException('The passwords were inconsistent twice!  ');
  }
}
// Receive different exceptions and handle them in a targeted manner!try {
  reg(array('email' => '1078789950@', 'pwd' => '123', 'repwd' => '1231' ));
} catch (Exception $e) {
  echo $e ->getMessage();
} catch (emailException $ee) {
  echo $ee ->getMessage();
} catch (pwdException $ep) {
  echo $ep;
}

For more information about PHP related content, please check out the topic of this site:Summary of PHP error and exception handling methods》、《Summary of usage of php strings》、《Complete collection of PHP array (Array) operation techniques》、《Summary of PHP operations and operator usage》、《Summary of PHP network programming skills》、《Introduction to PHP basic syntax》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.