It is very important to handle the errors by throwing a proper error message. MySQL provides a handler to handle the error. We can declare a handler with the help of the following syntax −
DECLARE handler_action FOR condition_value statement;
The above syntax shows that we need to use DECLARE HANDLER statement to declare a handler. If a condition whose value matches the condition_value then MySQL will execute the statement and continue or exit the current code block based on the action. Followings are the three major things in the above syntax −
Handler_action is of two types and can accept one of the following values −
Condition_value specifies a particular condition or class of conditions that activate the handler. It can accept one of the following values −
The Statement can be a simple statement or compound statement enclosing 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 then set the value of the got_error variable to 10 and continues the execution.