[Tutor] my own error code when no argument is sent

Alan Gauld alan.gauld at btinternet.com
Tue Jun 17 09:23:02 CEST 2008


<broek at cc.umanitoba.ca> wrote

>> infile = sys.argv[1]
>>
>> IndexError: list index out of range
>> is there a way that i could suppress this and add my own error code

> Hi Bryan,
> You are probably better off pursuing Danny's suggestion. But, this 
> is  what you asked for:
>
> IDLE 1.1.4
>>>> class CustomError(Exception):
> pass


Or, combining the suggestions you get:

import sys
try:
    infile = sys.argv[1]
except IndexError:
    # handle the error by assigning a default value
    # just as you would do if checking len()

Lots of options :-)

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list