How to catch str exception?

A

anuraguniyal

import sys
try:
raise "xxx"
except str,e:
print "1",e # is not caught here
except:# is caught here
print "2",sys.exc_type,sys.exc_value

In the above code a string exception is raised which though deprecated
but still a 3rd party library I use uses it. So how can I catch such
exception without relying on catch all, which could be bad.

system: Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3
(Ubuntu 4.2.3-2ubuntu7)] on linux2
 
K

Kurt Symanzik

import sys
try:
raise "xxx"
except str,e:
print "1",e # is not caught here
except:# is caught here
print "2",sys.exc_type,sys.exc_value

In the above code a string exception is raised which though deprecated
but still a 3rd party library I use uses it. So how can I catch such
exception without relying on catch all, which could be bad.

system: Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3
(Ubuntu 4.2.3-2ubuntu7)] on linux2

Try this, i.e. catch the exact string:

except "xxx":


Kurt
 
A

anuraguniyal

but the whole point of catching such exception is that i can print its
value
there are many such exceptions and hence it is not feasible to catch
them all or know them all unless i go thru src code.
 
P

Peter Otten

but the whole point of catching such exception is that i can print its
value
there are many such exceptions and hence it is not feasible to catch
them all or know them all unless i go thru src code.

Catch them all with a bare except and then reraise non-string exceptions:

try:
raise "abc"
except:
e, t, tb = sys.exc_info()
if not isinstance(e, str):
raise
print "caught", e
 
A

anuraguniyal

try:
    raise "abc"
except:
    e, t, tb = sys.exc_info()
    if not isinstance(e, str):
        raise
    print "caught", e

This seems to be the solution, thanks
 
D

Dave Angel

import sys
try:
raise "xxx"
except str,e:
print "1",e # is not caught here
except:# is caught here
print "2",sys.exc_type,sys.exc_value

In the above code a string exception is raised which though deprecated
but still a 3rd party library I use uses it. So how can I catch such
exception without relying on catch all, which could be bad.

system: Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3
(Ubuntu 4.2.3-2ubuntu7)] on linux2
Just fix the except type to be the exception caused by the illegal "raise"

except TypeError,e:
print "1",e # is caught here


1 exceptions must be classes or instances, not str
 
M

MRAB

but the whole point of catching such exception is that i can print its
value
there are many such exceptions and hence it is not feasible to catch
them all or know them all unless i go thru src code.
If string exceptions are so difficult to use, don't use them! :)

It would be better to write your own exception class:

class MyException(Exception):
pass

try:
raise MyException("Something bad happened!")
except MyException, e:
print "ERROR: %s" % e
 
A

anuraguniyal

It would be better to write your own exception class:

class MyException(Exception):
     pass


and how would i automatically inject this into 3rd part library
 
D

Dave Angel

doesn't happen so in 2.5.2
I tested it in 2.6.2

Perhaps you could try something like:

try:
raise "xxx"
except Exception, e:
print "1",e # is caught here
...and then test e.message and reraise if necessary
 
T

Terry Reedy

but the whole point of catching such exception is that i can print its
value
there are many such exceptions and hence it is not feasible to catch
them all or know them all unless i go thru src code.

I think you have discovered why they are gone in Py3 ;-).
 
P

Paul Rubin

there are many such exceptions and hence it is not feasible to catch
them all or know them all unless i go thru src code.

But that is true of all exceptions. The alternative seems to be a
"checked exception" scheme like Java's, which in practice imposes a
huge pain on programmers, who have to modify type signatures all up
and down the call hierarchy every time they raise another exception at
the bottom level.
 
M

MRAB

Paul said:
But that is true of all exceptions. The alternative seems to be a
"checked exception" scheme like Java's, which in practice imposes a
huge pain on programmers, who have to modify type signatures all up
and down the call hierarchy every time they raise another exception at
the bottom level.

I would've thought that a modern IDE would do a lot of the work for you.
 
R

Rhodri James

This seems to be the solution, thanks

Do be aware that string exceptions are going away, so at
some point you're going to upgrade your version of Python
and your scripts will mysteriously stop working. Peter's
patch will help you for now, but it's a double-edged sword
in that it also lets you pretend that you don't have to
fix the problem properly later. In my experience it's
generally best to fix the problem now rather than paper
over the cracks.

Since this is a third-party library doing this, as far as
I can see it you have three choices:

1) Get hold of a more up-to-date version of the module,
if there is one.

2) Ask the module writers nicely to fix the problem.

3) Fix it yourself.

The beauty of Python is that 3) isn't all that hard!
 

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
474,001
Messages
2,570,254
Members
46,849
Latest member
Fira

Latest Threads

Top