The try{}catch{} in PHP is used to handle exceptions. You can collect and display error messages for us. Hope that through the introduction of this article, we can grasp the application of this statement.
There are a lot of grammars in the PHP language that we need to be familiar with, and then we can use them flexibly and write the code programs we need. In this article we will introduce the usage of try{}catch{} in PHP.
<?php
try {
//...
} catch (Exception $e) {
//...
}
? >
try{}catch{} in PHP is exception handling.
The code to be executed is placed in a try block, and if one of the statements in the execution of the code has an exception, the program jumps directly into the catch block, where the error message and display are collected by $e.
PHP try{}catch{} statement
To further handle the exception, we need to use the PHP try{}catch{}----include the try statement and at least one of the catch statements. Any code that invokes a method that might throw an exception should use a try statement. Catch statements are used to handle exceptions that may be thrown.
Here's how we handle the exception thrown by Getcommandobject ():
<?php
try {
$mgr = new Commandmanager ();
$cmd = $mgr->getcommandobject ("Realcommand");
$cmd->execute ();
} catch (Exception $e) {
print $e->getmessage ();
Exit ();
>
As you can see, by using the Throw keyword in conjunction with PHP try{}catch{}, we can avoid the error marking the value returned by the "pollution" class method. Because "exception" itself is a type of PHP that is different from any other object, it does not create confusion.
If an exception is thrown, the script in the Try statement stops executing, and then immediately moves to the script in the catch statement.
If the exception is thrown and is not captured, a fatal error is generated.
The above article on the use of PHP try{}catch{} is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.