Home>

Exceptions are used to change the normal flow of a script when a specified error occurs. What is abnormal?PHP 5 provides a new object-oriented approach to error handling. Exception handling is used to change the normal flow of a script when a specified error (exception) situation occurs.This situation is called anomaly. When an exception is triggered,This usually happens:the current code state is saved, and code execution is switched to a predefined exception handler function.The processor may resume executing code from the saved code state,Terminate script execution,Or continue to execute the script from another place in the code. We will show different error handling methods:Basic use of exceptions. Create custom exception handlers. Multiple exceptions. Rethrow exceptions. Set top-level exception handlers.

Basic use of exceptions When exceptions are thrown,Subsequent code will not continue to execute,php will try to find a matching "catch" code block. If the exception is not caught,And without using set_exception_handler () for corresponding processing,Then a serious error (fatal error) will occur and an "uncaught exception" error message will be output. Let's try to throw an exception,Without catching it:

1)
 {
 throw new exception ("value must be 1 or below");
 }
 return true;
 }
//trigger exception
checknum (2);
?>

The above code will get an error similar to this:fatal error:uncaught exception "exception" with message "value must be 1 or below" in c:\ webfolder \ test.php:6 stack trace:#0 c:\ webfolder \ test.php (12):checknum (28) #1 {main} thrown in c:\ webfolder \ test.php on line 6 try, throw and catch To avoid the errors in the example above,We need to create the appropriate code to handle the exception. The handler should include:try-The function that uses the exception should be inside a "try" block.If no exception is triggered,The code will continue to execute as usual.But if the exception is triggered,Will throw an exception. throw-This specifies how to trigger an exception.Each "throw" must correspond to at least one "catch" catch-"catch" code blocks will catch exceptions,And create an object with exception information

Let's trigger an exception:

1)
 {
 throw new exception ("value must be 1 or below");
 }
 return true;
 }
//Trigger an exception in the "try" code block
try
 {
 checknum (2);
 //if the exception is thrown, this text will not be shown
 echo "if you see this, the number is 1 or below";
 }
//Catch exception
catch (exception $e)
 {
 echo "message:". $e->getmessage ();
 }
?>

The above code will get an error similar to this:message:value must be 1 or below Example explanation:The above code throws an exception,And captured it:

Create the checknum () function. It detects if the number is greater than 1. If so, an exception is thrown. The checknum () function is called in a "try" block. The exception in the checknum () function was thrown. The "catch" block received the exception,And create an object ($e) with exception information. By calling $e->getmessage () from this exception object, output the error message from the exception

However, in order to follow the "one catch per throw" principle, you can set up a top-level exception handler to handle missing errors. Creating a custom exception class Creating a custom exception handler is very simple.We simply created a specialized class,When an exception occurs in php,Its function can be called.This class must be an extension of the exception class.

This custom exception class inherits all the properties of PHP's exception class,You can add custom functions to it. We start to create the exception class:

getline (). "in". $this->getfile (). ":". $this->getmessage (). "is not a valid e-mail address";
 return $errormsg;
 }
 }
$email="[email protected] ... com";
try
 {
 //check if
 if (filter_var ($email, filter_validate_email) === false)
 {
 //throw exception if email is not valid
 throw new customexception ($email);
 }
 }
catch (customexception $e)
 {
 //display custom message
 echo $e->errormessage ();
 }
?>

This new class is a copy of the old exception class,Plus the errormessage () function. Because it is a copy of the old class,So it inherits properties and methods from the old class,We can use the methods of the exception class,Such as getline (), getfile () and getmessage (). Example explanation:The above code throws an exception,And catch it with a custom exception class:The customexception () class was created as an extension of the old exception class.This way it inherits all the properties and methods of the old class. Create the errormessage () function. If the e-mail address is invalid,Then the function returns an error message. Set the $email variable to an illegal e-mail address string. Execute the "try" code block. Since the e-mail address is illegal,So throw an exception "catch" code block to catch the exception,And show error message

Multiple exceptions You can use multiple exceptions for a script,To detect multiple situations. You can use multiple if..else blocks, or a switch block, or nest multiple exceptions.These exceptions can use different exception classes and return different error messages:

getline (). "in". $this->getfile ()
. ":". $this->getmessage (). "is not a valid e-mail address";
return $errormsg;
}
}
$email="[email protected]";
try
 {
 //check if
 if (filter_var ($email, filter_validate_email) === false)
 {
 //throw exception if email is not valid
 throw new customexception ($email);
 }
 //check for "example" in mail address
 if (strpos ($email, "example")! == false)
 {
 throw new exception ("$email is an example e-mail");
 }
 }
catch (customexception $e)
 {
 echo $e->errormessage ();
 }
catch (exception $e)
 {
 echo $e->getmessage ();
 }
?>

Example explanation:The above code tests two conditions,How any condition does not hold,An exception is thrown:

The customexception () class was created as an extension of the old exception class.This way it inherits all the properties and methods of the old class. Create the errormessage () function. If the e-mail address is invalid,The function returns an error message. Execute the "try" code block, and under the first condition,No exception is thrown. Since the e-mail contains the string "example", the second condition triggers an exception. "catch" blocks catch exceptions,And display the appropriate error message

If customexception is not caught and the base exception is caught tightly, the exception is handled there.

Rethrowing an exception Sometimes, when an exception is thrown,You might want to treat it differently than standard.You can throw an exception again in a "catch" block. The script should hide system errors from the user.For programmers,System errors may be important,But users are not interested in them.To make it easier for users,You can throw an exception again with a user-friendly message:

getmessage (). "is not a valid e-mail address.";
 return $errormsg;
 }
 }
$email="[email protected]";
try
 {
 try
 {
 //check for "example" in mail address
 if (strpos ($email, "example")! == false)
 {
 //throw exception if email is not valid
 throw new exception ($email);
 }
 }
 catch (exception $e)
 {
 //re-throw exception
 throw new customexception ($email);
 }
 }
catch (customexception $e)
 {
 //display custom message
 echo $e->errormessage ();
 }
?>

Example explanation:The above code checks whether the email address contains the string "example". If it does, it throws the exception again:

The customexception () class was created as an extension of the old exception class.This way it inherits all the properties and methods of the old class. Create the errormessage () function. If the e-mail address is invalid,The function returns an error message. Set the $email variable to a valid email address,But contains the string "example". The "try" block contains another "try" block so that the exception can be thrown again. The e-mail triggered the exception because it contains the string "example". "catch" caught the exception,And rethrow "customexception". "Customexception" is caught and an error message is displayed. If an exception is not caught in its current "try" block,Then it looks for catch blocks at a higher level. Set top exception handler The set_exception_handler () function sets a user-defined function that handles all uncaught exceptions.

getmessage ();
}
set_exception_handler ("myexception");
throw new exception ("uncaught exception occurred");
?>

The output of the above code should look like this:exception:uncaught exception occurred In the above code,There is no "catch" block. Instead, the top-level exception handler is triggered.This function should be used to catch all uncaught exceptions.

Exception rules The code that needs exception handling should be placed in a try block,To catch potential anomalies. Each try or throw block must have at least one corresponding catch block. Use multiple catch blocks to catch different kinds of exceptions. You can re-thrown exceptions in a catch block inside a try block.

In short:if an exception is thrown,You have to capture it.

php
  • Previous How to convert Chinese characters into pinyin based on PHP
  • Next Use UIScrollView to implement image carousel and click loading in iOS development