Some confusion about exception handling in php development?

when using business to deal with PHP problems, it is certain to take into account the interaction (mysql, redis, memcached.) between programs. The return value of.
generally speaking, for the business, we will consider what to do if the database insertion fails, what to do if the redis command fails to execute, which may be caused by the network timeout or the server crash, but when these situations occur, I often don"t know what to do on the program side, for example, database insertion failure, So does the inset method of pdo return a false or does the program report an exception directly? The network is disconnected when phpredis is operating redis. Does it return null or program exception? In the final analysis, it is unfamiliar with the return value of each method or the way the class library handles various reasons that may not succeed, which leads to the need to use a strict judgment such as if $return= 1 to determine the next program direction, resulting in untidy code.
has considered using try and catch to wrap the entire database insertion code segment, but is afraid that the failure to insert will return only $return=0,. That loses its meaning.
I would like to ask you, when dealing with the logic of interacting with mysql and redis, is there a unified memory method to judge whether the command execution is successful or failed? Or must remember through documentation that each method is executed for every error (network disconnection, server to be interacted with crashes, connection denied.) Can you only keep the return value in mind?

Php
Feb.26,2021

defaults to silent mode for MySQL, (neither warning nor exception is thrown).

clipboard.png

PDO


Redis

clipboard.png

so there is still a way to pass try. Catch to handle these problems gracefully.


this is how the error mechanism is handled.

first try. Catch.. is a standard development programming tool that must be done. Secondly, in catch, it is usually necessary to distinguish between error types, database errors, network errors, synthesis errors and so on. Then deal with it according to different errors. If you are linking with api, you usually have to return a response, other than 200, and then the api side has to make a request or the push is not successful, in preparation for the next push. If it is a non-api program, it is usually necessary to save the requested data for later reply. The practice of saving request data is usually to go to a completely separate database to avoid failure to log error logs due to host connection problems, which can be remote (this is recommended, borrowing third-party processing log), can be local. If the network is not available, the most original save is in the local file. If it is a very extreme situation, it is not the scope that the program should deal with.

Menu