Python - Exception

> Procedural Languages > Python > Python - Grammar (Package | Module)

1 - About

3 - Syntax

Exception handlers are specified with the try … except statement.

See:

Advertising

4 - Example

import sys
 
try:
    f = open('myfile.txt')
    s = f.readline()
    i = int(s.strip())
except OSError as err:
    print("OS error: {0}".format(err))
except ValueError:
    print("Could not convert data to an integer.")
except:
    print("Unexpected error:", sys.exc_info()[0])
    raise
finally:
    print("The finally clause is executed whether an exception occurred or not in the preceding code")

5 - Documentation / Reference