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:

o A MySQL error code (a number) or an SQLSTATE value (a 5-character
  string literal). You should not use MySQL error code 0 or SQLSTATE
  values that begin with '00', because those indicate success rather
  than an error condition. For a list of MySQL error codes and SQLSTATE
  values, see
  http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html.

o 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 is shorthand for the class of SQLSTATE values that begin
  with '01'.

o NOT FOUND is 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). For an example, see
  http://dev.mysql.com/doc/refman/5.5/en/cursors.html. This condition
  also occurs for SELECT ... INTO var_list statements that retrieve no
  rows.

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

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: http://dev.mysql.com/doc/refman/5.5/en/declare-handler.html

