: This article describes how to use PHPTry-catch statements. if you are interested in the PHP Tutorial, refer to it. PHP Try-catch statement
To further handle exceptions, we need to use the try-catch statement-including the Try statement and at least one catch statement. Any code that calls a method that may throw an exception should use the try statement. Catch statements are used to handle exceptions that may be thrown. The following shows how to handle the Exception thrown by getCommandObject:
<? Php try {$ mgr = new CommandManager (); $ cmd = $ mgr-> getCommandObject ("realcommand"); $ cmd-> execute ();} catch (Exception $ e) {print $ e-> getMessage (); exit () ;}?>
We can see that by combining the throw keyword with the try-catch statement, we can avoid the value returned by the error mark "pollution" class method. "Exception" is a built-in PHP type that is different from any other object and will not be confused.
If an exception is thrown, the script in the try statement stops execution and immediately redirects to the script in the catch statement.
If an exception is thrown but not captured, a fatal error occurs.
The above describes how to use PHP Try-catch statements, including the following content. I hope my friends who are interested in PHP tutorials will be helpful.