Python
  Home arrow Python arrow Page 7 - 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 - Raising The Bar


    (Page 7 of 9 )

    Thus far, you've been working with Python's built-in exceptions, which canhandle most logical or syntactical expressions. However, Python also allowsyou to get creative with exceptions, by generating your own customexceptions if the need arises.

    This is accomplished via Python's "raise" statement, which is used to raiseerrors which can be detected and resolved by the "try" family of exceptionhandlers. The "raise" statement needs to be passed an exception name, andan optional descriptive string. When the exception is raised, thisexception name and description will be made available to the definedexception handler.

    Let's go to a quick example - the line of code

    raise ValueError, "What on earth are you thinking?!"
    generates the following error.

    Traceback (innermost last): File "./test.py", line 3, in ? raise ValueError, "What on earth are you thinking?!" ValueError: What on earth are you thinking?!
    You can also name and use your own exceptions.

    #!/usr/bin/python

    import os

    # define error object error = "someError"

    # function to raise error def checkName(name): if (name != os.environ["USER"]): raise error, "Username mismatch!"

    name = raw_input("Enter your system username: ") checkName(name)
    In this case, if the username entered at the prompt does not match the namestored in the environment variable $USER, Python will raise a user-definedexception named "someError", with a string of text describing the nature ofthe error. Take a look:

    Enter your system username: john Traceback (innermost last): File "checkuser.py", line 16, in ? checkName(name) File "checkuser.py", line 11, in checkName raise error, "Username mismatch!" someError: Username mismatch!
    Note that the exception must be assigned to an object in order for it towork correctly.

    # define error object error = "someError"
    Trapping user-defined errors is exactly the same as trapping pre-definedPython errors. The following refinement of the code above illustrates this:

    #!/usr/bin/python

    import os

    # define error object error = "someError"

    # function to raise error def checkName(name): if (name != os.environ["USER"]): raise error, "Username mismatch!"

    # try this code try: name = raw_input("Enter your system username: ") checkName(name) except error, desc: print desc
    Here's the output of the script above, when the wrong username is entered.

    Enter your system username: john Username mismatch!

    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 3 hosted by Hostway
    Stay green...Green IT