1. Definitions
And java abnormal, are used to define the problems encountered in the process and the corresponding processing mode.
2, and custom exception handling
1, custom exception statement
DECLARE condition_name CONDITION FOR condition_value;
condition_name: from the name of their own, the best known name meaning see.
condition_value: may be SQLSTATE [VALUE] sqlstate_value may be mysql_error_value.
sqlstate_value error code is a string of 5.
mysql_error_value error code is a numeric type.
2, a custom exception handler
DECLARE handler_type HANDLER FOR condition_value sp_statement;
handler_type: There are three values
CONTINUE: An error was encountered no treatment over the code after the error continues.
EXIT: Exit immediately when an error is encountered, is no longer operating after execution.
UNDO: to withdraw before the operation has been executed after an error is encountered, MySQL does not support this operation.
condition_value: Error name: The following values
condition_name: take the definition of abnormal name
mysql_error_value: error code type value
SQLWARNING: shorthand for all SQLSTATE codes for the beginning of the 01's
NOT FOUND: shorthand for all SQLSTATE codes for the beginning of the 01's
SQLEXCEPTION: Shorthand SQLSTATE codes other than the SQLWARNING and NOT FOUND.
sp_statement: treatment after abnormal.
3, the example shows
Create the following table, the u_id as the primary key, and insert the following data:
Create a stored procedure called insert_user of:
BEGIN set @x = 1; insert into users values(3,'zhaoliu',26); set @x = 2; insert into users values(4,'zhouqi',27); set @x = 3; END
By using user variables @x to see whether the code is running, call a stored procedure
call insert_user();
The following error occurred
Where the number 1062 is mysql_error_code. Corresponding sqlstate_value is '23000', the specific correspondence table can be seen in a corresponding manner.
Check the value of @x
select @x;
We can see u_id = 3 of this statement appeared abnormal.
Next we add exception handling:
The BEGIN #Routine body goes here Wallpaper ... # 1: Direct Exception handling # DECLARE Exit 1062 the FOR HANDLER SET X1 = @. 4; # way: first custom exception, then the exception handling using the DECLARE ERROR1 CONDITION for 1062 ; DECLARE Exit HANDLER the FOR 1062 SET @ X1 = . 4 ; - handler_type first use herein Exit SET @x = . 1 ; INSERT INTO Users values ( . 3 , ' zhaoliu ' , 26 is ); SET @x = 2; insert into users values(4,'zhouqi',27); set @x = 3; END
Here To test the effect of exception handling we set the @ x1 is set to 4.
Call the stored procedure again would not be incorrect report
select @x,@x1;
Query User Variables
It found that when an exception occurs, the program will execute exception handling and exits the process.
The process handler_type changed to continue, repeat the procedure, it will not go wrong.
Query User Variables
After the discovery of the code for the following exception handling, we will continue to run
Queries users table
Abnormal insert statement is not executed, but the statement was no exception occurs behind the execution.