Skip to content

Latest commit

 

History

History
63 lines (49 loc) · 979 Bytes

File metadata and controls

63 lines (49 loc) · 979 Bytes

Back to Timetable

Exception handling

try:
    f = open("test")
    for line in f:
        print line
except :
    f = open("test.txt")
    for line in f:
        print line
try:  
    f = open("test.txt")
except :
    print "Cannot open the input file"
    raise SystemExit
else:
    for line in f:
      print line
def main(data):
    print data

try:
    import sys
    data = sys.argv[1]
except :
    print "<usage: python my_script.py arg1>"
    raise SystemExit
else:
    main(data)

Built-in exceptions

slot


def main(data):
    print data

try:
    import sys
    data = sys.argv[1]
except ImportError:
    print "<usage: python my_script.py arg1>"
    raise SystemExit
else:
    main(data)