Book Image

Python Essentials

By : Steven F. Lott
Book Image

Python Essentials

By: Steven F. Lott

Overview of this book

Table of Contents (22 chapters)
Python Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using the try and except statements


When an exception is raised, the ordinary sequential exception of statements stops. The next sequential statement is not executed. Instead, the exception handlers are examined to find an except clause which matches the given exception's class. This search proceeds down the call stack from the current function to the function which called it. If an except clause is found which matches the exception, then ordinary sequential execution resumes in that except clause. When the except clause finishes, the try statement is also finished. From there, the normal sequential statement execution continues after the try statement.

If no except clause matches the given exception, the exception and the traceback information is printed. Processing stops, and Python exits. Generally, the exit status is non-zero to indicate that the Python program ended abnormally.

A try statement inside a function looks like this:

def clean_number(text):
    try:
        value= float(text...