Syntax:
DECLARE handler_action HANDLER
    FOR condition_value [, condition_value] ...
    statement

handler_action: {
    CONTINUE
  | EXIT
  | UNDO
}

condition_value: {
    mysql_error_code
  | SQLSTATE [VALUE] sqlstate_value
  | condition_name
  | SQLWARNING
  | NOT FOUND
  | SQLEXCEPTION
}

The DECLARE ... HANDLER statement specifies a handler that deals with
one or more conditions. If one of these conditions occurs, the
specified statement executes. statement can be a simple statement such
as SET var_name = value, or a compound statement written using BEGIN
and END (see [HELP BEGIN END]).

Handler declarations must appear after variable or condition
declarations.

The handler_action value indicates what action the handler takes after
execution of the handler statement:

o CONTINUE: Execution of the current program continues.

o EXIT: Execution terminates for the BEGIN ... END compound statement
  in which the handler is declared. This is true even if the condition
  occurs in an inner block.

o UNDO: Not supported.

The condition_value for DECLARE ... HANDLER indicates the specific
condition or class of conditions that activates the handler. It can
take the following forms:

o mysql_error_code: An integer literal indicating a MySQL error code,
  such as 1051 to specify "unknown table":

DECLARE CONTINUE HANDLER FOR 1051
  BEGIN
    -- body of handler
  END;

  Do not use MySQL error code 0 because that indicates success rather
  than an error condition. For a list of MySQL error codes, see Server
  Error Message Reference
  (https://dev.mysql.com/doc/mysql-errors/8.4/en/server-error-reference
  .html).

o SQLSTATE [VALUE] sqlstate_value: A 5-character string literal
  indicating an SQLSTATE value, such as '42S01' to specify "unknown
  table":

DECLARE CONTINUE HANDLER FOR SQLSTATE '42S02'
  BEGIN
    -- body of handler
  END;

  Do not use SQLSTATE values that begin with '00' because those
  indicate success rather than an error condition. For a list of
  SQLSTATE values, see Server Error Message Reference
  (https://dev.mysql.com/doc/mysql-errors/8.4/en/server-error-reference
  .html).

o condition_name: A condition name previously specified with DECLARE
  ... CONDITION. A condition name can be associated with a MySQL error
  code or SQLSTATE value. See [HELP DECLARE CONDITION].

o SQLWARNING: Shorthand for the class of SQLSTATE values that begin
  with '01'.

DECLARE CONTINUE HANDLER FOR SQLWARNING
  BEGIN
    -- body of handler
  END;

o NOT FOUND: Shorthand for the class of SQLSTATE values that begin with
  '02'. This is relevant within the context of cursors and is used to
  control what happens when a cursor reaches the end of a data set. If
  no more rows are available, a No Data condition occurs with SQLSTATE
  value '02000'. To detect this condition, you can set up a handler for
  it or for a NOT FOUND condition.

DECLARE CONTINUE HANDLER FOR NOT FOUND
  BEGIN
    -- body of handler
  END;

  For another example, see
  https://dev.mysql.com/doc/refman/8.4/en/cursors.html. The NOT FOUND
  condition also occurs for SELECT ... INTO var_list statements that
  retrieve no rows.

o SQLEXCEPTION: Shorthand for the class of SQLSTATE values that do not
  begin with '00', '01', or '02'.

DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
  BEGIN
    -- body of handler
  END;

For information about how the server chooses handlers when a condition
occurs, see https://dev.mysql.com/doc/refman/8.4/en/handler-scope.html.

If a condition occurs for which no handler has been declared, the
action taken depends on the condition class:

o For SQLEXCEPTION conditions, the stored program terminates at the
  statement that raised the condition, as if there were an EXIT
  handler. If the program was called by another stored program, the
  calling program handles the condition using the handler selection
  rules applied to its own handlers.

o For SQLWARNING conditions, the program continues executing, as if
  there were a CONTINUE handler.

o For NOT FOUND conditions, if the condition was raised normally, the
  action is CONTINUE. If it was raised by SIGNAL or RESIGNAL, the
  action is EXIT.

URL: https://dev.mysql.com/doc/refman/8.4/en/declare-handler.html

