Detailed usage of try {} catch {} in PHP. In try {...} catch (Exception $ e ){...} try {} catch {} in PHP is exception handling. put the code to be executed into the TRY block. if an exception occurs in a statement during the code execution
Try
{//...}
Catch (Exception $ e)
{//...}
Try {} catch {} in PHP is exception handling.
The code to be executed is placed in the TRY block. if an exception occurs in a statement during code execution, the program jumps directly to the CATCH block, and the error information and display are collected by $ e.
Try {} catch {} statement in PHP
To further handle exceptions, we need to use try {} catch {} in PHP to include Try statements 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();
- }
- ?>
As you can see, by combining the throw keyword with try {} catch {} in PHP, we can avoid the error of marking the value returned by the "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.
Please try {//...} catch (Exception $ e ){//...} try {} catch {} in PHP is exception handling. put the code to be executed into the TRY block. if an exception occurs in a statement during the code execution, then...