Is it possible to get the Exception that was previously caught by some library in Python 2.7? The library does something like this:
try:
something_that_throw_exception
except Exception, e:
raise Exception("Error", e)
and I would like to get the type or instance of e.
Related
Consider
try:
import someProprietaryModule
except ImportError:
raise ImportError('It appears that <someProprietaryModule> is not installed...')
When run, if someProprietaryModule is not installed, one sees:
(traceback data)
ImportError: unknown module: someProprietaryModule
During handling of the above exception, another exception occurred:
(traceback data)
ImportError: It appears that <someProprietaryModule> is not installed...
Perhaps I don't want the "During handling of the above exception..." line (and the lines above it) to appear. I could do this:
_moduleInstalled = True
try:
import someProprietaryModule
except ImportError:
_moduleInstalled = False
if not _moduleInstalled:
raise ImportError('It appears that <someProprietaryModule> is not installed...')
But that feels like a bit of a hack. What else might I do?
In Python 3.3 and later raise ... from None may be used in this situation.
try:
import someProprietaryModule
except ImportError:
raise ImportError('It appears that <someProprietaryModule> is not installed...') from None
This has the desired results.
This can be done like this in Python 2.7 and Python 3:
try:
import someProprietaryModule
except ImportError as e:
raised_error = e
if isinstance(raised_error, ImportError):
raise ImportError('It appears that <someProprietaryModule> is not installed...')
You can try logging module as well
Original Answer:
Perhaps I don't want the "During handling of the above exception..." line (and the lines above it) to appear.
import logging
try:
import someProprietaryModule
except Exception as e:
if hasattr(e, 'message'):
logging.warning('python2')
logging.error(e.message)
else:
logging.warning('python3')
logging.error('It appears that <someProprietaryModule> is not installed...')
gives
WARNING:root:python3
ERROR:root:It appears that <someProprietaryModule> is not installed...
[Program finished]
Edit:
import logging
class MyExceptionType(Exception):
"""Base class for other exceptions"""
pass
try:
from someProprietaryModule import *
except Exception as e:
logging.warning('python3')
logging.exception("Failed to import <someProprietaryModule>. Is it installed?", exc_info=False)
raise MyExceptionType from e
logging.exception will emit the stacktrace alongside the localized error message, which makes it quite useful.
Casting an exception to a string to print it removes 90% of the useful information.
Silently suppressing exceptions is almost always an error and is most commonly a footgun.
In an except block I want to raise the same exception but without the stack trace and without the information that this exception has been raised as direct cause of another exception. (and without modifying sys.tracebacklimit globally)
Additionally I have a very clumsy exception class which parses and modifies the message text so I can't just reproduce it.
My current approach is
try:
deeply_nested_function_that_raises_exception()
except ClumsyExceptionBaseClass as exc:
cls, code, msg = exc.__class__, exc.code, exc.msg
raise cls("Error: %d %s" % (code, msg))
What I'm doing here is de-composing the exception information, re-assemble a new exception with a message which will be parsed and split into error code and message in the constructor and raise it from outside the except block in order to forget all trace information.
Is there a more pythonic way to do this? All I want is get rid of the noisy (and useless in my case) trace back while keeping the information contained in the exception object..
In Python 3, you can use with_traceback to remove the traceback entries accumulated so far:
try: ...
except Exception as e:
raise e.with_traceback(None)
In Python 2, it’s just
try: ...
except Exception as e:
raise e # not just "raise"
It will of course still show the trace to this line, since that’s added as the exception propagates (again).
I have written Python code which does some calculation. During this it converts string to float. However sometimes numeric string value may be empty that time its giving me valueError. I tried to keep that in try catch block however its going to another exception block as shown below.
try:
float(some value)
except Exception as ValueError:
print(error message)
except Exception as oserror:
print(mesage)
Its going to os error block instead of ValueError block
That's not how you capture exceptions.
try:
float(some value)
except ValueError as e:
print("here's the message", e.args)
except OSError as e:
print("here's a different message")
(Note, though, there's no instance when calling float would raise an OSError.)
I need to catch a specific OperationalError exception. The exception text uses the error-code 2006. The library defines the error-codes at MySQLdb.constants.CR.SERVER_GONE_ERROR = 2006.
How do you get the error-code from the exception?
When I check the MySQLdb._mysql_exceptions, there is a definition of the OperationalError exception but it has no constructor or description of how to access the exception error code.
You can catch the error number like the following:
try:
# Adding field 'Bug.bize_size_tag_name'
db.add_column('search_bug', 'bize_size_tag_name', orm['search.bug:bize_size_tag_name'])
except MySQLdb.OperationalError, errorCode:
if errorCode[0] == 1060:
pass
else:
raise
Reference: https://www.programcreek.com/python/example/2584/MySQLdb.OperationalError
I am bit confused about the try exception usage in Python 2.7.
try:
raise valueError("sample value error")
except Exception as e:
print str(e)
try:
raise valueError("sample value error")
except Exception,exception:
print str(exception)
try:
raise valueError("sample value error")
except exception:
print str(exception)
try:
raise valueError("sample value error")
except Exception:
print str(Exception) # it prints only the object reference
can some help me to understand the above usage?
Some concepts to help you understand the difference between the alternate variants of the except variants:
except Exception, e – This in an older variant, now deprecated, similar to except Exception as e
except Exception as e – Catch exceptions of the type Exception (or any subclass) and store them in the variable e for further processing, messaging or similar
except Exception – Catch exceptions of the type Exception (or any subclass), but ignore the value/information provided in the exception
except e – Gives me an compilation error, not sure if this related to python version, but if so, it should/would mean that you don't care about the type of exception but want to access the information in it
except – Catch any exception, and ignore the exception information
What to use, depends on many factors, but if you don't need the provided information in the exception there is no need to present the variable to catch this information.
Regarding which type of Exception to catch, take care to catch the accurate type of exceptions. If you are writing a general catch it all, it could be correct to use except Exception, but in the example case you've given I would opt for actually using except ValueError directly. This would allow for potentially other exceptions to be properly handled at another level of your code. The point is, don't catch exception you are not ready to handle.
If you want, you can read more on python 2.7 exception handling or available python 2.7 exception in the official documentation.
For Python 3 (also works in Python 2.7):
try:
raise ValueError("sample value error")
except Exception as e:
print(e)
For Python 2 (will not work in Python 3):
try:
raise ValueError("sample value error")
except Exception, e:
print e
I use:
try:
raise valueError("sample value error")
except Exception as e:
print str(e)
When I want to declare a specific error and
try:
raise valueError("sample value error")
except:
print "Something unexpected happened"
When I don't really care or except: pass , except: return etc
Use the format
try:
raise ValueError("sample value error")
except Exception, e:
print e