Home >Database >Mysql Tutorial >In MySQL, how do we declare a handler when handling errors?

In MySQL, how do we declare a handler when handling errors?

PHPz
PHPzforward
2023-08-23 09:37:061051browse

In MySQL, how do we declare a handler when handling errors?

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 −

Syntax of handler

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:

    • CONTINUE - If handler_action is 'CONTINUE', execution of the enclosing code block continues.
    • EXIT - If handler_action is 'EXIT', execution of the enclosing code block is terminated.
  • Condition_valueSpecifies the specific condition or condition category that activates the handler. It can accept the following values:

    • ERROR CODE - condition_value can be a MySQL error code.
    • SQLSTATE - condition_value can also be SQLSTATE.
    • SQLWARNING - condition_value can also be SQLWARNING.
    • NOTFOUND - condition_value can also be NOTFOUND.
    • SQLEXCEPTION - condition_value can also be SQLEXCEPTION.
  • Statement can be a simple statement or a compound statement enclosed by the BEGIN and END keywords.

Example

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete