MySQL
  Home arrow MySQL arrow Error Handling Examples
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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: 4 stars4 stars4 stars4 stars4 stars / 3
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    Error Handling Examples
    (Page 1 of 4 )

    In this second article in a three-part series, you will learn more about errors and exception handlers. It is excerpted from chapter six of the book MySQL Stored Procedure Programming, written by Guy Harrison and Steven Feuerstein (O'Reilly; ISBN: 0596100892). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    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 aNOT FOUND), exit the current block or stored program after issuing aROLLBACK 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 theSELECTstatement (which generates a message for the calling program):

        DECLARE CONTINUE HANDER FOR 1062 
            SELECT 'Duplicate key in index';
    • IfSQLSTATE23000 (duplicate key value) is encountered, continue execution after executing theSELECT statement (which generates a message for the calling program):

        DECLARE CONTINUE HANDER FOR SQLSTATE '23000'
            SELECT 'Duplicate key in index';
       
    • When a cursor fetch or SQL retrieves no values, continue execution after settingl_done=1:

        DECLARE CONTINUE HANDLER FOR NOT
      FOUND
            SET l_done=1; 
    • Same as the previous example, except specified using aSQLSTATEvariable rather than a named condition:

        DECLARE CONTINUE HANDLER FOR SQLSTATE '02000'
            SET l_done=1; 
    • Same as the previous two examples, except specified using a MySQL error code variable rather than a named condition orSQLSTATE variable:

        DECLARE CONTINUE HANDLER FOR 1329
            SET l_done=1;

    More MySQL Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "MySQL Stored Procedure Programming,"...
     

    Buy this book now. This article is excerpted from chapter six of the book MySQL Stored Procedure Programming, written by Guy Harrison and Steven Feuerstein (O'Reilly; ISBN: 0596100892). Check it out today at your favorite bookstore. Buy this book now.

       

    MYSQL ARTICLES

    - 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...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...
    - Creating the Blog Script for a PHP/MySQL Blo...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway