Understanding error handling syntax and how to implement error handling

In order to handle errors, you can use special error handling. The syntax for that is as follows: 

DECLARE action HANDLER FOR condition statement;

When a condition matches the condition specified in the handler, MySQL will execute the statement specified in the handler and either continue or exit based on the action specified. This statement can be a single statement or multiple statements surrounded by BEGIN and END

The action specified in the handler can be one of two values: 

  • Continue: Execution of the code will continue 
  • Exit: Execution of the code will terminate 

The condition specified in the handler can be one of the following: 

  • A MySQL error code: This will be an integer value for a specific error, such as 1051. More information on error codes can be found in the Further reading section. 
  • A standard SQLSTATE value: This will be a five-character string value, such as 42S01. This could also be SQLWARNING, NOTFOUND, or SQLEXCEPTION. More information on these can be found in the Further reading section.

Let's look at an example, for ease of understanding. You can execute the following to create a stored procedure with error handling: 

USE lahmansbaseballdb;
DELIMITER $$
CREATE PROCEDURE insertallstarfull(
IN inplayerid varchar(9),
IN inyearid smallint,
IN ingamenum smallint
)
BEGIN
DECLARE EXIT HANDLER FOR 1062
BEGIN
SELECT CONCAT('Duplicate key (',inplayerid,',',inyearid,',',ingamenum,') occurred') AS message;
END;

INSERT INTO allstarfull (playerid, yearid, gamenum)
VALUES (inplayerid, inyearid, ingamenum);

SELECT count(*)
FROM allstarfull
WHERE playerid = inplayerid;
END$$
DELIMITER ;

The previous stored procedure will allow us to insert values into allstarfull by passing in playerid, yearid, and gamenum as parameters.

You can call it with the following query: 

CALL insertallstarfull('aaronha01', 1958, 0); 

The preceding call will produce an error, as follows:

Unlike if you didn't have error handling in your stored procedure, it won't throw an error and show that error in the Output area of MySQL Workbench. In this case, it shows you the error you custom coded into the stored procedure in your query window and gracefully exits the stored procedure.

Instead, if we call the stored procedure with different parameter values, as shown in the following query, we won't receive an error message: 

CALL insertallstarfull('aaronha01', 1954, 0); 

We now get the count(*) results from the query that follows in the insert in the stored procedure, as shown in the following screenshot:

Declaring and handling errors is considered catching errors. With this method, you have more control over how errors are handled instead of just letting a stored procedure fail and having to find the error message in the output panel. This can be especially beneficial if you want to continue with an error or capture the errors to write them to a table. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.132.97