[Tutor] Creating a class and calling an exception

Alan Gauld alan.gauld at btinternet.com
Fri Jun 3 10:05:49 CEST 2011


"Becky Mcquilling" <ladymcse2000 at gmail.com> wrote

> The Second script written here, always raises the exception and I'm 
> missing
> why, any advice?
> class Log_Parser:
>    def __init__(self):
>        self.re_backup_status = re.compile(r'^\s+Files\s+:\s+\d',
> re.IGNORECASE)
>
>    def log_parse(self, log_file):
>
>      try:
> ....
>            for line in log1:
>              if re_backup_status.search(line):

When accessing an attribute of a class you need to prefix with self.
You are getting a name error here I suspect.

>        log1.close()
>      except:  print "%s" %('succeeded:0 failed:10000')

But your cattchall exception is hiding the error. If you add
a raise inside the exception handler while debugging it
will print the full error text.

If you did that you would have been pointed to the
line with the error and told the name of the bad name.
Using a catchall exceptioon is OK after the code is
working if you want to shield your users from tracebacks,
but during debugging catchall exceptions are evil,
don't do it.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list