From http://www.devshed.com/c/a/MySQL/Error-Handling-Examples/
Error handler examples
Here are some examples of handler declarations:
If any error condition arises (other than a not found), continue execution after setting l_error = 1:
Declare continue handler for sqlexception
Set l_error = 1;
If any error condition arises (other than a not found), exit the current block or stored program after issuing a rollback statement and issuing an error message:
Declare exit handler for sqlexception
Begin
Rollback;
Select 'error occurred-terminating ';
End;
If MySQL error 1062 (duplicate key value) is encountered, continue execution after executing the SELECT statement (which generates a message for the calling program ):
Declare continue hander for 106 2
Select 'duplicate key in Index ';
If sqlstate 23000 (duplicate key value) is encountered, continue execution after executing the SELECT statement (which generates a message for the calling program ):
Declare continue hander for sqlstate '20140901'
Select 'duplicate key in Index ';
When a cursor fetch or SQL retrieves no values, continue execution after setting l_done = 1:
Declare continue handler for not
Found
Set l_done = 1;
Same as the previous example, except T specified using a sqlstate variable rather than a named condition:
Declare continue handler for sqlstate '20140901'
Set l_done = 1;
Same as the previous two examples, counter t specified using a MySQL error code variable rather than a named condition or sqlstate variable:
Declare continue handler for 1329
Set l_done = 1;
Error Handling example
There are several types of error handling statements:
§ If any error (not found) occurs, set l_error to 1 and continue execution:
Declare continue handler for sqlexception
Set l_error = 1;
§ If any error occurs (not found), execute rollback and generate an error message to exit the current block or stored procedure.
Declare exit handler for sqlexception
Begin
Rollback;
Select 'error occurred-terminating ';
End;
§ If a MySQL 1062 error (duplicate key value) occurs, execute the SELECT statement (to call Program Send a message) and continue execution
Declare continue hander for 106 2
Select 'duplicate key in Index ';
§ If the sqlstate 2300 error (duplicate key value) occurs, execute the SELECT statement (send a message to the calling program) and continue to execute
Declare continue hander for sqlstate '20140901'
Select 'duplicate key in Index ';
§ When no return value is returned for a cursor or SQL selection statement, set l_done = 1 and continue execution.
Declare continue handler for not
Found
Set l_done = 1;
§ In this example, apart from using the sqlstate variable rather than the naming condition, this is the same as in the previous example.
Declare continue handler for sqlstate '20140901'
Set l_done = 1;
§ In this example, besides using the MySQL error code variable instead of the naming condition or sqlstate variable
Declare continue handler for 1329
Set l_done = 1;