MySQL
  Home arrow MySQL arrow Page 3 - Error Handling Examples
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Error Handling Examples
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2007-09-06


    Table of Contents:
  • Error Handling Examples
  • Handler Precedence
  • Scope of Condition Handlers
  • Named Conditions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Error Handling Examples - Scope of Condition Handlers
    ( Page 3 of 4 )

    The scope of a handler determines which statements within the stored program are covered by the handler. In essence, the scope of a handler is the same as for a stored program variable: the handler applies to all statements in the block in which it is defined, including any statements in nested blocks. Furthermore, handlers in a stored program also cover statements that execute in any stored program that might be called by the first program, unless that program declares its own handler.

    For instance, in Example 6-10 the handler will be invoked when the INSERT statement executes (because it violates a NOT NULL constraint). The handler fires because the INSERT statement is contained within the same block as the handler—even though the INSERT statement is in a nested block.

    Example 6-10. Handler scope includes statements within BEGIN-END blocks

    DECLARE CONTINUE HANDLER FOR 1048 SELECT 'Attempt to insert a null value';
    BEGIN
       
    INSERT INTO departments (department_name,manager_id,location)
       
    VALUES (NULL,1,'Wouldn''t you like to know?');
    END;

    However, in Example 6-11 the handler will not be invoked—the scope of the handler is limited to the nested block, and the INSERT statement occurs outside that block.

    Condition Handlers

    Example 6-11. Handlers within a nested block do not cover statements in enclosing blocks

    BEGIN
       
    BEGIN
            DECLARE CONTINUE HANDLER
    FOR 1216 select
                     
    'Foreign key constraint violated';
        END;
        INSERT INTO departments (department_name,manager_id,location)
             VALUES ('Elbonian HR','Catbert','Catbertia');
    END;

    Handler scope extends to any stored procedures or functions that are invoked within the handler scope. This means that if one stored program calls another, a handler in the calling program can trap errors that occur in the program that has been called. So, for instance, in Example 6-12, the handler in calling_procedure() traps the null value exception that occurs in sub_procedure() .

    Example 6-12. A handler can catch conditions raised in called procedures

    CREATE PROCEDURE calling_procedure()
    BEGIN
      DECLARE EXIT HANDLER FOR 1048 SELECT 'Attempt to insert a null value';
      CALL sub_procedure();
    END;

    Query OK, 0 rows affected (0.00 sec)

    --------------
    CREATE PROCEDURE sub_procedure()
    BEGIN
      INSERT INTO departments (department_name,manager_id,location)
      VALUES (NULL,1,'Wouldn''t you like to know');
      SELECT 'Row inserted';

    END;

    Query OK, 0 rows affected (0.00 sec)

    CALL calling_procedure();

    +--------------------------------+
    | Attempt to insert a null value |
    +--------------------------------+
    | Attempt to insert a null value |
    +--------------------------------+
    1 row in set (0.01 sec)

    Query OK, 0 rows affected (0.01 sec)

    Of course, a handler in a procedure will override the scope of a hander that exists in a calling procedure. Only one handler can ever be activated in response to a specific error condition.



     
     
    >>> More MySQL Articles          >>> More By O'Reilly Media
     

       

    MYSQL ARTICLES

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT