Regular expression and exception

Mr.SpOOn mr.spoon21 at gmail.com
Sat Nov 15 09:20:05 EST 2008


Hi,
I've never used exception before, but I think now it's time to start.

I've seen that there is a list of  the built-in exceptions in the
Python docs, but this explains the meaning of every exception. Does
exist an inverted list? I mean, how may I know what kind of exception
is going to raise my code? Shall I figure out by myself?

Apart from this, in a method I check valid strings with a regular
expression. I pass the string to the method and then Ii have something
like:

m = re.match('[1-9]$', my_string)

I was thinking to put a try except here, so that:

try:
    m = re.match('[1-9]$', my_string)
except:
    print 'something...'

But actually this doesn't work, because re.match just assign None to
m, without raising anything.

The only way seems to check with m.group(), so after I matched the
string I should have:

try:
   m.group()
except:
   print 'error...'

Or shall I put also the matching inside the try? Or am I completely wrong?



More information about the Python-list mailing list