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

handler_type:
    CONTINUE
  | EXIT
  | UNDO

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

The DECLARE ... HANDLER statement specifies handlers that each may deal
with one or more conditions. If one of these conditions occurs, the
specified statement is executed. statement can be a simple statement
(for example, SET var_name = value), or it can be a compound statement
written using BEGIN and END (see [HELP BEGIN END]).

For a CONTINUE handler, execution of the current program continues
after execution of the handler statement. For an EXIT handler,
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.) The UNDO handler type statement is not supported.

If a condition occurs for which no handler has been declared, the
default action is EXIT.

A condition_value for DECLARE ... HANDLER can be any of the following
values:

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

o A condition name previously specified with DECLARE ... CONDITION. 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 only within the context of cursors and is
  used to control what happens when a cursor reaches the end of a data
  set.

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

URL: http://dev.mysql.com/doc/refman/5.1/en/declare-handler.html

