Home > Database > Mysql Tutorial > body text

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

PHPz
Release: 2023-08-23 09:37:06
forward
977 people have browsed it

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;
Copy after login

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;
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template