It is very important to handle errors and throw appropriate error messages. MySQL provides a handler to handle errors. We can declare a handler using the following syntax −
DECLARE handler_action FOR condition_value statement;
The above syntax shows that we need to use the DECLARE HANDLER statement to declare a handler. If the value of a condition matches condition_value, then MySQL will execute the statement and continue or exit the current block of code depending on the action. Here are the three main takeaways from the above syntax:
Handler_action has two types and can accept the following values:
Condition_valueSpecifies the specific condition or condition category that activates the handler. It can accept the following values:
Statement can be a simple statement or a compound statement enclosed by the BEGIN and END keywords.
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET got_error = 1;
In the above example, a handler means that if an error occurs, set the value of got_error variable to 10 and continue execution.
The above is the detailed content of In MySQL, how do we declare a handler when handling errors?. For more information, please follow other related articles on the PHP Chinese website!