The use and description of PHP's exception handling class Exception

  • 2020-05-17 04:57:40
  • OfStack

1. First of all, php5 provides a basic exception handling class, which can be used directly
 
<?php 
class Exception 
{ 
protected $message = 'Unknown exception'; //  Exception information  
protected $code = 0; //  User-defined exception code  
protected $file; //  The file name of which an exception occurred  
protected $line; //  The line number where the exception occurred  
function __construct($message = null, $code = 0); 
final function getMessage(); //  Return exception information  
final function getCode(); //  Return exception code  
final function getFile(); //  Returns the file name where the exception occurred  
final function getLine(); //  Returns the line number of the code where the exception occurred  
final function getTrace(); // backtrace()  An array of  
final function getTraceAsString(); //  Latticed into a string  getTrace()  information  
/*  An overloaded method  */ 
function __toString(); //  A string that can be output  
} 
?> 

Simple use :(throws error message via exception)
 
try { 
$error = 'my error!'; 
throw new Exception($error) 
} catch (Exception $e) { 
echo $e->getMessage(); 
} 

2. We can extend this class for our convenience
 
class MyException extends Exception 
{ 
//  Redefine the constructor to  message  Becomes a property that must be specified  
public function __construct($message, $code = 0) { 
//  Custom code  
//  Make sure all variables are assigned correctly  
parent::__construct($message, $code); 
} 
//  Customize the style of the string output  
public function __toString() { 
return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; 
} 
public function customFunction() { 
echo "A Custom function for this type of exception\n"; 
} 
} 

The basic idea of exception handling is that code is called to execute in try code. If an error occurs in the try block, we can perform an exception handling. Some programming languages, such as java, automatically throw exceptions in certain situations. In php, exceptions must be thrown manually. You can throw an exception in the following manner:
Throw new Exception (' message ', code);
The Throw keyword will trigger the exception handling mechanism, which is a language construct, not a function, but must be passed a value. It requires one accept object. In the simplest case, you can instantiate one of the built-in Exception classes.
Finally, after the try code, you must give at least one catch code block. Multiple catch code blocks can be associated with one try code block. If each catch code block can catch one different type of exception, it makes sense to use multiple catch code blocks. For example, if you wanted to catch an exception for the Exception class, the code would look like this
 
Catch(Exception $e) 
{ 
//handing exception 
} 
Catch The object that the code captures is the one that causes the exception and passes it to throw The object of the statement (by throw  Statement thrown). use Exception  Class, is a good choice.  
Exception Class provides the following built-in methods:  
  Getcode()    Returns the code passed to the constructor.  
  GetMessage()  Returns the message passed to the constructor.  
  getFile()     Returns the path to the file that generated the exception code  
  getLine() Returns the line of code that produced the exception.  

Note:
When an exception is caught, the subsequent code inside the try() block will not continue to execute, but will try to find a match for the "catch" block
When an exception is thrown, the "Uncaught exception 'Exception'" error is reported if catch processing is not performed
 
<?php 
function test($val){ 
if ($val>100){ 
throw new Exception(" Prompt information : The value you entered is too large "); 
} 
} 
test(111); 
?> 

3. When an exception is thrown, the catch block can be processed or not
The following is part of the code of my user registration function
 
try{ 
//check forms filled in 
if(!filled_out($_POST)){ 
throw new Exception(' You haven't filled out the form yet, please go back and fill it out '); 
} 
//check email address not valid 
if(!check_email($email)){ 
throw new Exception(' The format of the message is incorrect '); 
} 
// Check if the length of the density is greater than 6 
if(strlen($passwd<6)){ 
throw new Exception(' The length of the density should be greater than 6'); 
} 
// Check that the two passwords are equal  
if($passwd!=$passwd1){ 
throw new Exception(' Two passwords no 1 Please re-enter the sample '); 
} 
// Check that the username is the right length  
if(strlen($username)>16){ 
throw new Exception(' The length of the username does not match , Please re-enter '); 
} 
} catch(Exception $e){ 
echo $e->getMessage(); // Output exception information.  
} 

Related articles: