Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Built-in Exceptions - Page 3

September 14, 2001

Table 5.1
ExceptionDescription
ExceptionThe root of all exceptions
SystemExitGenerated by sys.exit()
StandardErrorBase for all built-in exceptions
ArithmeticErrorBase for arithmetic exceptions
FloatingPointErrorFailure of a floating-point operation
OverflowErrorArithmetic overflow
ZeroDivisionErrorDivision or modulus operation with 0
AssertionErrorRaised by the assert statement
AttributeErrorRaised when an attribute name is invalid
EnvironmentErrorErrors that occur externally to Python
IOErrorI/O or file-related error
OSErrorOperating system error
WindowsErrorError in Windows
EOFErrorRaised when the end of the file is reached
ImportErrorFailure of the import statement
KeyboardInterruptGenerated by the interrupt key (usually Ctrl+C)
LookupErrorIndexing and key errors
IndexErrorOut-of-range sequence offset
KeyErrorNonexistent dictionary key
MemoryErrorOut of memory
NameErrorFailure to find a local or global name
UnboundLocalErrorUnbound local variable
RuntimeErrorA generic catch-all error
NotImplementedErrorUnimplemented feature
SyntaxErrorParsing error
TabErrorInconsistent tab usage (generated with -tt option)
IndentationErrorIndentation error
SystemErrorNonfatal system error in the interpreter
TypeErrorPassing an inappropriate type to an operation
ValueErrorInappropriate or missing value
UnicodeErrorUnicode encoding error

The try statement also supports an else clause, which must follow the last except clause. This code is executed if the code in the try block doesn't raise an exception. Here's an example:

try:
  f = open('foo','r')
except IOError:
  print 'Unable to open foo'
else:
  data = f.read()
  f.close()

The finally statement defines a cleanup action for code contained in a try block. For example:

f = open('foo','r')
try:
  #Do some stuff
  ...
finally:
  f.close()
  print "File closed regardless of what happened."

The finally clause isn't used to catch errors. Rather, it's used to provide code that must always be executed, regardless of whether an error occurs. If no exception is raised, the code in the finally clause is executed immediately after the code in the try block. If an exception occurs, control is first passed to the first statement of the finally clause. After this code has executed, the exception is re-raised to be caught by another exception handler. The finally and except statements cannot appear together within a single try statement.

Python defines the built-in exceptions listed in Table 5.1. (For specific details about these exceptions, see Appendix A.)

All the exceptions in a particular group can be caught by specifying the group name in an except clause. For example,

try:
  statements
except LookupError:  # Catch IndexError or KeyError
  statements

or

try:
  statements
except StandardError:  # Catch any built-in exception
  statements

Exceptions - Page 2
Python Essential Reference, Second Edition
Defining New Exceptions - Page 4


Up to => Home / Authoring / Languages / Python / Ref




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers