Regular expression and exception

M

Mr.SpOOn

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?
 
A

Arnaud Delobelle

Mr.SpOOn said:
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?

The interactive prompt will help you!

E.g. I want to know what exception arises when I try to add an int to a
string:
Traceback (most recent call last):

Ah, it's a TypeError!
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?

In this case re.match has been designed to return None if there are no
matches so there is no need to look for exceptions. This is probably
because it is very common to use re.match(...) while expecting no match
so it is not really an 'exception'. Just do

m = re.match(...)
if m is None:
print 'error...'
else:
# Do something with m

HTH
 
B

bearophileHUGS

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

Generally don't write a nude except, use qualified exceptions, that is
put there one of more exceptions that you want to catch (be careful
with the syntax with more than one exception, see the docs).

Bye,
bearophile
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top