Exceptions and Error Trapping in Python (Autosaved)
Exceptions and Error Trapping in Python (Autosaved)
Handling
• We now consider exception and error handling and
how it is implemented in Python
• The traditional method of handling errors in most of the languages is to
check for return value by a function. The return value will be a positive
integer, representing a true value if the function call works.
• There are few problems with this method of error trapping
• We can handle this by wrapping the call to runcalc within a try statement and providing an except clause.
• try:
• try:
• remote.send_data(destination,datastream)
• finally:
• remote.close_connection()
• except NetworkError,errorstr:
• Print(“error: couldnot send the data:”, errorstr)
else:
print(“data sent successfully”)
• Now the connection is closed whether or not an
exception occurs, while the outer try statement traps
and handles the actual exception raised during the
process
Exceptions Nest
• So exceptions are always handled by the enclosing exception handler
• But what happens if your exception handler doesn’t explicitly handle
every possible exception?
• The answer is quite simple: the exception is raised to the next higher
level exception
Built in Exceptions
• Python comes with a number of base exceptions and exception
classes.
• All of these exceptions can be used and trapped within your scripts to
indicate or identify an error
Exception
• This is the root class used for all exceptions
StandardError
• StandardError is the base class used for all the
built-in exceptions. StandardError inherits the
facilities offered by the Exception root class
ArithmeticError
• For exceptions arising due to arithmetic errors, one of the
specific arithmetic exceptions (OverFlowError,
ZeroDivisionError, FloatingPointError) is raised .
ArithmetiticError is the base class used by all the three
exceptions to indicate a general arithmetic fault.
• Since it is base class , you can use this exception to trap all
the the three arithmetic errors
AssertionError
• AssertionError is the exception raised when an assert statement fails
AttributeError
• AttributeError is the exception raised when an attribute
reference or assignment fails
EnvironmentError
• EnvironmentError is the class for errors that occur outside of Pythons
control, that can be traced to the environment in which Python is
operating
EOFError
• EOFError is the exception raised when the endof file condition is
detected by the built in data handling functions
FloatingPointError
• The FloatingPointError exception is raised when a floating point
operation fails
ImportError
• The importError exception is raised when an import statement fails to
find the specified module or when from fails to find the specific
symbol in the module
IndexError
• The IndexError exception is raised when you try to access a sequence
element out of the range of sequences size
IOError
• The IOError exception is raised when an I/O operation fails, for
example trying to open a nonexistant file or trying to write to a device
with no free space
KeyError
• The keyError exception is raised when the dictionary or other
mapping key requested does not exist within the mapping object
LookupError
• LookupError is the base exception class for the built –in IndexError
and KeyError exceptions. This exception is used to indicate an error
when accessing information from a sequence (string,list or tuple) or
mapping (dictionary)
MemoryError
• The MemoryError exception is raised when the interpreter runs out of
memory while executing a specific option , but still thinks it can
recover from the situstion if some objects are deleted to free up
memory space.
Other Built-in exceptions
• KeyboardInterupt SystemError
• NameError SystemExit
• NotImplementedError TypeError
• OSError UnboundLocalError
• OverflowError UnicodeError
• RuntimeError
• SyntaxError
• In Python the terms Error and Exception are used inter-
changeably;