Getting arg value from generic exception

Paul Rubin phr-n2001d at
Sun Oct 14 03:15:48 CEST 2001

Suppose I want to catch an generic exception and get the value--
is there a clean way to do that?  I.e., for a specific
exception FrobError, I can say

    try:  x = frob()
    except FrobError, arg: report_error(arg)
But what about generic exceptions:

   try: x = frob()
   except: ...  # how do I get the arg?

Using sys.exc_info works, but doesn't feel too good: exceptions with
args are supposed to be a Python language feature and not a library
function.  Also, the sys module must of course never be exposed to
code running under restricted execution.  Even sys.exc_info probably
shouldn't be exposed, since if restricted code can see the exc_info[2]
stack trace, it may be able to reach the local vars of bastion objects
that throw exceptions (I haven't checked this).

There's a trend in Python towards making all exceptions inherit from
the Exception class, which would solve the problem (just say except
Exception,arg: ...) but for now you can still have exceptions that
don't inherit from Exception.  

So this looks like a gap in the Python spec.  Am I missing something?

More information about the Python-list mailing list