BEGIN -- outer block DECLARE EXIT HANDLER FOR ...; -- handler H1 DECLARE EXIT HANDLER FOR ...; -- handler H2 stmt1 ; stmt2 ; END; 

•A handler is in scope only for the block in which it is declared, and cannot be activated for conditions occurring outside that block. In the following example, handler H1  is in scope for stmt1  in the inner block, but not for stmt2  in the outer block: 

BEGIN -- outer block BEGIN -- inner block DECLARE EXIT HANDLER FOR ...; -- handler H1 stmt1 ; END; stmt2 ; END; 

•A handler can be specific or general. A specific handler is for a MySQL error code, SQLSTATE  value, or condition name. A general handler is for a condition in the SQLWARNING , SQLEXCEPTION , or NOT FOUND  class. Condition specificity is related to condition precedence, as described later. 

Multiple handlers can be declared in different scopes and with different specificities. For example, there might be a specific MySQL error code handler in an outer block, and a general SQLWARNING  handler in an inner block. Or there might be handlers for a specific MySQL error code and the general SQLWARNING class in the same block. 

Whether a handler is activated depends not only on its own scope and condition value, but on what other handlers are present. When a condition occurs in a stored program, the server searches for applicable handlers in the current scope (current BEGIN ... END  block). If there are no applicable handlers, the search continues outward with the handlers in each successive containing scope (block). When the server finds one or more applicable handlers at a given scope, it chooses among them based on condition precedence: 

•A MySQL error code handler takes precedence over an SQLSTATE  value handler. 

•An SQLSTATE  value handler takes precedence over general SQLWARNING , SQLEXCEPTION , or NOT FOUND  handlers. 

•An SQLEXCEPTION  handler takes precedence over an SQLWARNING  handler. 

•It is possible to have several applicable handlers with the same precedence. For example, a statement could generate multiple warnings with different error codes, for each of which an error-specific handler exists. In this case, the choice of which handler the server activates is nondeterministic, and may change depending on the circumstances under which the condition occurs. 

One implication of the handler selection rules is that if multiple applicable handlers occur in different scopes, handlers with the most local scope take precedence over handlers in outer scopes, even over those for more specific conditions. 

If there is no appropriate handler when a condition occurs, the action taken depends on the class of the condition: 

•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. 

•For SQLWARNING  conditions, the program continues executing, as if there were a CONTINUE  handler. 

