Python
  Home arrow Python arrow Page 2 - Python 101 (part 8): An Exceptionally ...
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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? 
PYTHON

Python 101 (part 8): An Exceptionally Clever Snake
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 8
    2001-08-23

    Table of Contents:
  • Python 101 (part 8): An Exceptionally Clever Snake
  • Anatomy Of An Exception
  • Trying Harder
  • Different Strokes
  • Passing The Buck
  • Bad Boys
  • Raising The Bar
  • Strong Pythons (And The Exceptions That Love Them)
  • The End Of The Affair

  • 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


    Python 101 (part 8): An Exceptionally Clever Snake - Anatomy Of An Exception


    (Page 2 of 9 )

    No developer, no matter how good (s)he is, writes bug-free code all thetime. Which is why most programming languages - including Python - comewith built-in capabilities to catch errors and take remedial action. Thisaction could be something as simple as displaying an error message, or ascomplex as heating your computer's innards until they burst into flame(just kidding!)

    Normally, when a Python program encounters an error, be it syntactical orlogical, it exits the program at that stage itself with a messageindicating the cause of the error. Now, while this behaviour is acceptableduring the development phase, it cannot continue once a Python program hasbeen released to actual users. In these "live" situations, it isunprofessional to display cryptic error messages (which are usuallyincomprehensible to non-technical users); rather, it is more professionalto intercept these errors and either resolve them (if resolution ispossible), or notify the user with a clear error message (if not).

    The term "exceptions" refers to those errors which can be tracked andcontrolled. For example, if a function attempts an unsupported operation ona built-in Python object (say, modifying an immutable object), Python willgenerate a "TypeError" exception, together with a stack trace or detailedexplanation of the problem. Exceptions like these can be caught by theapplication, and appropriately diverted to an exception-handling routine.

    An example might make this clearer. Consider the following Python program,

    #!/usr/bin/python

    # set up tuple dessert = ('apple pie', 'chocolate fudge cake', 'icecream')

    # change tuple value dessert[1] = 'chocolate brownies'
    which generates a TypeError because I'm trying to modify a tuple.

    Traceback (innermost last): File "dessert.py", line 5, in ? dessert[1] = 'chocolate brownies' TypeError: object doesn't support item assignment
    Now, let's add some code to trap and handle the exception gracefully.

    #!/usr/bin/python

    try: # try running this code dessert = ('apple pie', 'chocolate fudge cake', 'icecream') dessert[1] = 'chocolate brownies' except TypeError: # if error, do this... print "Whoops! Something bad just happened. Terminating script."
    And this time, when the script is executed, it will not generate aTypeError - instead, it will output

    Whoops! Something bad just happened. Terminating script.
    This is a very basic example of how Python exceptions can be trapped, andappropriate action triggered. As you will see, the ability to handle errorsin a transparent manner throws open some pretty powerful possibilities...

    More Python Articles
    More By Vikram Vaswani, (c) Melonfire


     

       

    PYTHON ARTICLES

    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT