Programming Basics
  Home arrow Programming Basics arrow Page 6 - Coding
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
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? 
PROGRAMMING BASICS

Coding
By: lig
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2006-01-17

    Table of Contents:
  • Coding
  • Thinking before Coding
  • Commenting
  • Coding Style
  • Error Reporting
  • Error Handling
  • Security
  • Reinventing the Wheel
  • Advanced
  • Conclusion

  • 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


    Coding - Error Handling


    (Page 6 of 10 )

    Murphy's Law dictates "what can go wrong will." Servers lose electricity, connections time out, paths lead to nothing and hard drives crash. These are things outside of our scripts' control. It is the programmer's job to give scripts a graceful way of handling these and other problems. And let us not forget to mention error handling can help us debug a problem by letting us know there is a problem and where.

    My examples are in the PHP4 procedural style--but these concepts can also be done in the Object Oriented style. For example, there is a PEAR_Error class and PHP5 has full support for exceptions and the try/catch structure. Use them if that is an option you want.

    Example 1

    Error handling when interacting with a database is a common area often overlooked by new programmers. Most new programmers get the connection and selection query, but when fetching results throws an error they have no idea what went wrong. Error handling along the way will tell you EXACTLY where the problem first occurred. A simple example using a MySQL database:

    <?php
    $link 
    mysql_connect($host$usr$pass);
    // handle connection error
    if(!$link)
    {
      
    // handle the error â€“ echo to the screen, log it, redirect, whatever
      // if we are debugging tell me what went wrong
      // otherwise hide it from malicious users
      
    if($debug
      { 
        echo 
    'Error: DB connection '.mysql_error();
      }
      
    // exit gracefully
      
    exit;
    }
    ?>

    Example 2

    What about those that want to do things with their files--read, write, upload or download?

    <?php
    $basepath 
    '/home/usr/www/downloads/';
    switch(
    $_POST['type'])
    {
      case 
    'audio':
        
    $type 'audio/';
        break;
      case 
    'video'
        
    $type 'vid/';
        break;
      
    // catch anything
      
    default:
        
    $type 'lyric/';
    }
    $path $basepath.$type.$user.'/';
    // handle path error
    if(!is_dir($path))
    {
      
    // handle the error â€“ echo to the screen, log it, redirect, whatever
      // go back to the form or exit gracefully
    }
    ?>

    More Programming Basics Articles
    More By lig


       · The link to Chris Shiflett's blog is broken.The blog can be found at...
       · Be careful when you log stuff to rotate the logfile, otherwise it'll fill up and...
       · Sorry about that.
       · Thanks very much for taking the time to write this. Although most was over my head,...
       · That was pretty convincing Gospel. I hope this stuff is gonna work for...
     

    PROGRAMMING BASICS ARTICLES

    - PHP: Hypertext Preprocessor: What is it?
    - Loops and PHP Decision Making
    - Operators, Conditionals, and PHP Decision-Ma...
    - PHP Decision-Making
    - Coding
    - Server Statistics
    - Looping in PHP
    - Cookies in PHP
    - Working with text files
    - Beginning Object Oriented Programming in PHP
    - A Tour of Decision Making Structures in PHP
    - PHP Strings Primer
    - PHP Control Structures
    - Intro to Vim
    - Reading Directorys with PHP





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek