Multiple catch blocksĪ try.catch statement can have multiple catch blocks. In this example, we get the detailed error message by calling the getMessage() method of the $ex object. The exception variable $ex is an instance of the Exception class that contains the detailed information of the error. In this example, if any error occurs in the try.block, the execution jumps to the catch block. Here’s a typical syntax of the try.catch statement: getMessage() To handle the exceptions, you use the try.catch statement. Instead of halting the script, you can handle the exceptions gracefully. Exceptions can be attempting to read a file that doesn’t exist or connecting to the database server that is currently down. In programming, unexpected errors are called exceptions. Introduction to the PHP try…catch statement The function is called if all attempts failed: maximum number of attempts reached, or we got an exception which is considered not temporary (by callback function defined in setIsTemporaryException()).Summary: in this tutorial, you will learn how to use the PHP try.catch statement to handle exceptions. > setIsTemporaryException( function( \ Throwable $e): bool So let's take a look on the next example which contains this special logic: $request = new \ GuzzleHttp\ Psr7\ Request(" GET", " ") If it returns false it will immediately stop trying. If it returns true (error is temporary) then new attempt will be performed (except if maximum number of attempts reached). The RetryHelper allows you to define callback function which will be called after each unsuccessful attempt and will decide is this error (exception) is temporary or not. All 400-th statuses means that problem is on client side (wrong password, no access, wrong URL, etc.) Thereby we can divide all errors on "temporary" (which may go away on next try) and "permanent" (which will not disappear). Generally, you don't need to try again if you receive HTTP 4xx status code. For example, if you have an "authentication error", there's not much reason to try again and again with the same credentials. If you want to only try x times, you can replace the retry with a int with a startvalue of number of tries first. If some exception occurs, it will run the catch and finally block, and then loop back up and run the try block again. By default, it will retry on every error (exception), no matter what kind of error. As long as the last line of the try block gets run ( retry false ), it will carry on. Since this is the simplest example, it uses some default behaviour which we will override in the next examples. The exception thrown inside the anonymous function at the last attempt will be re-thrown to the top. If your Internet completely doesn't work or the website is down, once the maximum number of attempts is reached, the execution will be terminated. But if you have unstable Internet connection, it may take several attempts to get a response. In most cases this code will execute successfully on the first try, the return value of the anonymous function will be redirected to the return value of the execute() method and immediately saved in the $response variable. The second argument defines maximum number of attempts (in this example 10). In this example the code that may fail here is wrapped into anonymous function and passed as the first argument of execute() method. Return ( new \ GuzzleHttp\ Client())-> send( $request) Įcho $response-> getBody()-> getContents(). $response = ( new \gugglegum\ RetryHelper\ RetryHelper()) Here is the simplest example that tries to get response from an HTTP server using the GuzzleHttp package, up to 10 attempts: $request = new \ GuzzleHttp\ Psr7\ Request(" GET", " ") Therefore, if your code doesn't throw an exception on error (for example, if you use curl_exec() function which simply returns false on error), then you need to check return value and throw an exception inside this function. This method determines the error only by the exception thrown inside the anonymous function. A section of potentially problematic code should be wrapped in an anonymous function ( Closure, callable) and passed to the execute() method. It's quite flexibly configured through the use of callback functions and supports standardized PHP-FIG/PSR-3 logging. This simple package contains the RetryHelper class, which simplifies error handling, retries, delaying and logging. a request to a remote server), you may need some kind of error handling, retrying, make a delay between attempts, and stop if the maximum number of retries reached. When you perform some action that may not succeed on the first try (e.g.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |