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

broek at cc.umanitoba.ca broek at cc.umanitoba.ca
Tue Jun 17 09:01:46 CEST 2008




> when i execute my program without an argument i receive,
>
>
> 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

>>> import sys
>>> try:
	infile = sys.argv[1]
except IndexError:
	raise CustomError, "Some msg (this is optional)"


Traceback (most recent call last):
   File "<pyshell#12>", line 4, in -toplevel-
     raise CustomError, "Some msg (this is optional)"
CustomError: Some msg (this is optional)


Best,

Brian vdB


More information about the Tutor mailing list