Exception handling

What if the user fails to provide two command-line arguments?
Python aborts execution with an informative error message
A good alternative is to handle the error manually inside the program code:
try:
    infilename  = sys.argv[1]
    outfilename = sys.argv[2]
except:
    # try block failed,
    # we miss two command-line arguments
    print 'Usage:', sys.argv[0], 'infile outfile'
    sys.exit(1)
This is the common way of dealing with errors in Python, called exception handling

previousnexttable of contents