代码如下 |
复制代码 |
class Exception
{
protected $message = 'Unknown exception'; // 异常信息
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(); // 可输出的字符串
}
?>
|
class Exception
{
Protected $message = 'Unknown exception'; // Exception message
protected $code = 0; //User-defined exception code
protected $file; // The name of the file where the exception occurred
protected $line;
function __construct($message = null, $code = 0);
代码如下 |
复制代码 |
try {
$error = 'my error!';
throw new Exception($error)
} catch (Exception $e) {
echo $e->getMessage();
}
|
final function getMessage(); // Return exception message
final function getCode(); // Return exception code
final function getFile(); // Returns the file name where the exception occurred
final function getLine(); // Returns the code line number where the exception occurred
final function getTrace(); // backtrace() array
final function getTraceAsString(); // getTrace() information that has been formatted into a string
/* Overloadable methods */
function __toString(); // Outputable string
}
?>
Simple usage is as follows: (Throw error message through exception)
The code is as follows |
Copy code |
try {
$error = 'my error!';
Throw new Exception($error)
} catch (Exception $e) {
echo $e->getMessage();
}
|
We can extend this class to facilitate our use
The code is as follows |
Copy code |
代码如下 |
复制代码 |
class MyException extends Exception
{
// 重定义构造器使 message 变为必须被指定的属性
public function __construct($message, $code = 0) {
// 自定义的代码
// 确保所有变量都被正确赋值
parent::__construct($message, $code);
}
// 自定义字符串输出的样式
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}n";
}
public function customFunction() {
echo "A Custom function for this type of exceptionn";
}
}
|
class MyException extends Exception
{
// Redefine the constructor so that message becomes a property that must be specified
Public function __construct($message, $code = 0) {
//Customized code
// Make sure all variables are assigned correctly
parent::__construct($message, $code);
}
代码如下 |
复制代码 |
Throw new Exception(‘message’,code);
|
// Customize the style of string output
Public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}n";
}
public function customFunction() {
echo "A Custom function for this type of exceptionn";
}
}
|
The basic idea of exception handling is that the code is called and executed in the try code. If an error occurs in the try code block, we can perform an exception processing. Some programming languages, such as Java, will automatically throw exceptions under certain circumstances. In PHP, exceptions must be thrown manually. An exception can be thrown using the following method:
The code is as follows |
Copy code |
代码如下 |
复制代码 |
Catch(Exception $e)
{
//handing exception
}
|
Throw new Exception(‘message’,code);
|
The Throw keyword will trigger the exception handling mechanism. It is a language structure, not a function, but a value must be passed to it. It requires a receiving object. In the simplest case, one can instantiate a built-in Exception class.
Finally, after the try code, at least one catch code block must be given. You can associate multiple catch code blocks with a try code block. It makes sense to use multiple catch blocks if each catch block can catch a different type of exception. For example, if you want to catch exceptions of the Exception class, the code is as follows
The code is as follows |
Copy code |
Catch(Exception $e)
{
//handing exception
}
|
The object captured by the Catch code is the object that caused the exception and was passed to the throw statement (thrown by the throw statement). Using an instance of the 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 generated the exception.
Note:
When an exception is caught, subsequent code in the try() block will not continue to execute, but will try to find a matching "catch" code block
When an exception is thrown, if no catch processing is performed, the "Uncaught exception 'Exception'" error will be reported
The code is as follows |
Copy code |
|
代码如下 |
复制代码 |
function test($val){
if ($val>100){
throw new Exception("提示信息:您输入的值过大");
}
}
test(111);
?>
|
Function test($val){
If ($val>100){
throw new Exception("Prompt message: The value you entered is too large");
}
}
Test(111);
?>
3. When an exception is thrown, the catch statement block can be processed or not processed
The following is part of the code for my user registration function
try{
//check forms filled in
If(!filled_out($_POST)){
throw new Exception('You haven't filled in the form yet, please go back and fill it in');
}
//check email address not valid
If(!check_email($email)){
throw new Exception('The format of the email is incorrect');
}
//Check whether the length of density is greater than 6
If(strlen($passwd<6)){
throw new Exception('The length of the density should be greater than 6');
}
//Check whether the two passwords are equal
If($passwd!=$passwd1){
throw new Exception('The two passwords are different, please re-enter');
}
//Check whether the length of the username is correct
If(strlen($username)>16){
throw new Exception('The length of the user name does not match, please re-enter');
}
} catch(Exception $e){
Echo $e->getMessage(); //Output exception information.
}
php handles exceptions the same as java, using try{}catch(){}
The function used to define the top-level exception handler is
set_exception_handler("My_exception");
My_expection here is a developer-defined exception handling function, which is a top-level exception handler. Only when there is no function in the program to handle exceptions will there be a top-level exception handler to handle exceptions. If no top-level exception handler is defined, the system will default to it. Exception handler to handle exceptions
Example:
The code is as follows
代码如下 |
复制代码 |
set_exception_handler("My_expection");
function My_expection(){
echo "这里是顶级异常处理器";
}
try{
nohello("hello");
}catch(Exception $e){
throw $e;
}
function nohello($nohello){
if($nohello == "hello"){
throw new Exception("不能输入hello");
}else{
echo "输入成功";
}
}
?> |
|
Copy code
|
set_exception_handler("My_expection"); |
Function My_expection(){
echo "Here is the top-level exception handler";
}
Try{
nohello("hello");
}catch(Exception $e){
throw $e;
}
function nohello($nohello){
if($nohello == "hello"){
throw new Exception("Cannot enter hello");
}else{
echo "Input successful";
}
}
?>
http://www.bkjia.com/PHPjc/631616.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631616.htmlTechArticleThis article introduces the simple exception handling classes commonly used in php. We mainly talk about Exception handling. There are Students who need to know more can refer to it. The code is as follows Copy the code ?ph...
|