Right now, I catch the exception in the except Exception: clause, and do print(exception). The result provides no information since it always prints <class 'Exception'>. I knew this used to work in python 2, but how do I do it in python3?
I'm guessing that you need to assign the Exception to a variable. As shown in the Python 3 tutorial:
def fails():
x = 1 / 0
try:
fails()
except Exception as ex:
print(ex)
To give a brief explanation, as is a pseudo-assignment keyword used in certain compound statements to assign or alias the preceding statement to a variable.
In this case, as assigns the caught exception to a variable allowing for information about the exception to stored and used later, instead of needing to be dealt with immediately.
(This is discussed in detail in the Python 3 Language Reference: The try Statement.)
There are other compound statements that use as. The first is the with statement:
#contextmanager
def opening(filename):
f = open(filename)
try:
yield f
finally:
f.close()
with opening(filename) as f:
# ...read data from f...
Here, with statements are used to wrap the execution of a block with methods defined by context managers. This functions like an extended try...except...finally statement in a neat generator package, and the as statement assigns the generator-produced result from the context manager to a variable for extended use.
(This is discussed in detail in the Python 3 Language Reference: The with Statement.)
As of Python 3.10, match statements also use as:
from random import randint
match randint(0, 2):
case 0|1 as low:
print(f"{low} is a low number")
case _:
print("not a low number")
match statements take an expression (in this case, randint(0, 2)) and compare its value to each case branch one at a time until one of them succeeds, at which point it executes that branch's block. In a case branch, as can be used to assign the value of the branch to a variable if that branch succeeds. If it doesn't succeed, it is not bound.
(The match statement is covered by the tutorial and discussed in detail in the Python 3 Language Reference: match Statements.)
Finally, as can be used when importing modules, to alias a module to a different (usually shorter) name:
import foo.bar.baz as fbb
This is discussed in detail in the Python 3 Language Reference: The import Statement.
These are the changes since python 2:
try:
1 / 0
except Exception as e: # (as opposed to except Exception, e:)
# ^ that will just look for two classes, Exception and e
# for the repr
print(repr(e))
# for just the message, or str(e), since print calls str under the hood
print(e)
# the arguments that the exception has been called with.
# the first one is usually the message. (OSError is different, though)
print(e.args)
You can look into the standard library module traceback for fancier stuff.
Try
try:
print(undefined_var)
except Exception as e:
print(e)
this will print the representation given by e.__str__():
"name 'undefined_var' is not defined"
you can also use:
print(repr(e))
which will include the Exception class name:
"NameError("name 'undefined_var' is not defined",)"
Here is the way I like that prints out all of the error stack.
import logging
try:
1 / 0
except Exception as _e:
# any one of the follows:
# print(logging.traceback.format_exc())
logging.error(logging.traceback.format_exc())
The output looks as the follows:
ERROR:root:Traceback (most recent call last):
File "/PATH-TO-YOUR/filename.py", line 4, in <module>
1 / 0
ZeroDivisionError: division by zero
LOGGING_FORMAT :
LOGGING_FORMAT = '%(asctime)s\n File "%(pathname)s", line %(lineno)d\n %(levelname)s [%(message)s]'
Although if you want a code that is compatible with both python2 and python3 you can use this:
import logging
try:
1/0
except Exception as e:
if hasattr(e, 'message'):
logging.warning('python2')
logging.error(e.message)
else:
logging.warning('python3')
logging.error(e)
[In Python3]
Let's say you want to handle an IndexError and print the traceback, you can do the following:
from traceback import print_tb
empty_list = []
try:
x = empty_list[100]
except IndexError as index_error:
print_tb(index_error.__traceback__)
Note: You can use the format_tb function instead of print_tb to get the traceback as a string for logging purposes.
Hope this helps.
Don't use print(e), since that won't print a stack trace, which is a nightmare for debugging. traceback.print_exception is what you're looking for:
import traceback
try:
assert False
except Exception as e:
traceback.print_exception(e)
I've use this :
except (socket.timeout, KeyboardInterrupt) as e:
logging.debug("Exception : {}".format(str(e.__str__).split(" ")[3]))
break
Let me know if it does not work for you !!
You can do:
with self.assertRaisesMessage(ValueError, 'invalid literal for int()'):
int('a')
Reference
Related
I have a function like this
def try_strip(s):
try:
return s.strip()
except Exception as e:
print(e)
# (I've tried inspect, traceback, logging, sys)
and if I call it somewhere like this
try_strip('could be a string or not')
then the exception line number would be the one in which the try_strip is defined.
Is there a way to get info about where was it called? Thank you in advance.
The Traceback module which is included with Python provides this functionality. According to its documentation it:
provides a standard interface to extract, format and print stack traces of Python programs. It exactly mimics the behaviour of the Python interpreter when it prints a stack trace.
The function traceback.format_stack() will return the stack trace information you need as a list of strings while the function traceback.print_stack() will print the stack trace information to the console. Below I have included some code which shows how you might use this in the example you provided:
import traceback
def try_strip(s):
try:
return s.strip()
except Exception as e:
traceback.print_stack()
stack_trace_info = traceback.format_stack()
# Code that write stack_trace_info to a log could go here
try_strip(5) # This will cause an error at runtime
For additional info on the Traceback module, please see https://docs.python.org/3/library/traceback.html .
Problem:
Sometimes it is nice to be able to remove or apply a try statement temporarily. Is there a convenient way to disable the try statement without re-indenting?
For example, if there was a python block statement equivalent called "goForIt:" one could edit the word "try:" to "goForIt:" and it would just execute the block as though it were not wrapped in a "try" and ignore the "except" line too.
The problem I'm trying to solve is that while I want the try statement in production I want to be able to remove it temporarily while debugging to see the error traceback rather than have it trap and process the exception.
Currently I work around this by commenting out the "try" then re-indent the code in the block. Then comment out the entire "except" block. This seems clumsy.
Instead of removing the try, you could make the except re-raise the exception:
try:
raise ValueError('whoops')
except ValueError as e:
raise # <-- just put this here
print('caught')
This will raise the error, just as if it were not caught:
ValueError Traceback (most recent call last)
<ipython-input-146-a6be6779c161> in <module>
1 try:
----> 2 raise ValueError('whoops')
3 except ValueError as e:
4 raise
5 print('caught')
ValueError: whoops
I do not believe there is a way to fix this.
Is it adequate to catch the exception, print it out and, if wanted, end the program?
try:
# code
except Exception as e:
print(e)
# end program if wanted
If you want a none code solution you need to use an IDE or a good text editor.
I am using Visual Studio code where I can indent with a keyboard shortcut (Ctrl + ` in my case)
(This is not my answer, but the answer from Alani's comment. If credits, find the original comment under the question.)
The solution is good because it allows global replacement:
try: --> if 1: # try:
except --> if 0: # except
The 2nd one is a little unsure. Exact and full word match should be used, or replace twice (for except<space> and except:). Or you can fix error fast by hand if there is any.
The replacement back is sure.
I need now such solution to debug error in strawberry which is only printed. So I think I need deactivate all try/except structures in 3 libraries (strawberry + 2 libs for django).
I've seen it many times but never understood what the as command does in Python 3.x. Can you explain it in plain English?
It's not a command per se, it's a keyword used as part of the with statement:
with open("myfile.txt") as f:
text = f.read()
The object after as gets assigned the result of the expression handled by the with context manager.
Another use is to rename an imported module:
import numpy as np
so you can use the name np instead of numpy from now on.
The third use is to give you access to an Exception object:
try:
f = open("foo")
except IOError as exc:
# Now you can access the Exception for more detailed analysis
It is a keyword used for object naming in several cases.
from some_module import something as some_alias
# `some_alias` is `some_module.something`
with open("filename") as f:
# `f` is the file object `open("filename")` returned
try:
Nonsense!
except Exception as e:
# `e` is the Exception thrown
My Django app is currently throwing this error on one of my pages, does anyone know what it eans? I would supply more detail but I don't know what this error means so I'm not sure what the relevant files are and Django apps are rather large in the amount of code spread around so I'll post some code once I can get an idea of what this means. Thanks in advance for any help.
EDIT: I tried capturing the error and printing it like so:
EDIT: I've entered the code that's throwing the error
jobIDs is a dict containing all of the IDs of the records I want to modify
for i in jobIDs:
dateToRun = request.POST['dateToRun']
timeToRun = request.POST['timeToRun']
try:
if len(request.POST['dateToRun']) <= 0:
dateToRun = Job.objects.filter(id=jobIDs[i]).values()['whenToRun'].split(' ')[0]
if len(request.POST['timeToRun']) <= 0:
timeToRun = Job.objects.filter(id=jobIDs[i]).values()['whenToRun'].split(' ')[1]
except BaseException, e:
print str(e)
whenToRun = dateToRun + ' ' + timeToRun
Job.objects.filter(id=jobIDs[i]).update(whenToRun=whenToRun)
This produces a blank line of output (from the print in the except block), am I misunderstanding how to print out the error?
Are you executing a piece of code that may throw an exception? Perhaps a database query for something that does not exist? If so, you will need to wrap the block of code in a try/except clause. For example, if the exception is indeed a query for something that does not exist:
try:
#Block of code that throws exception
except Object.DoesNotExist:
#Handle error
Perhaps it's because you're using BaseException instead of just Exception? Try this:
try:
# Your code that may throw an exception
except Exception, e:
print str(e)
As per the Python Exception docs on BaseException:
exception BaseException
directly inherited by user-defined classes (for that, use Exception)The base class for all built-in exceptions. It is not meant to be directly inherited by user-defined classes (for that, use Exception).
I wrote a method that does some stuff and catches bad filenames. what should happen is if the path doesn't exist, it throws an IOError. however, it thinks my exception handling is bad syntax... why??
def whatever():
try:
# do stuff
# and more stuff
except IOError:
# do this
pass
whatever()
but before it even gets to calling whatever(), it prints the following:
Traceback (most recent call last):
File "", line 1, in
File "getquizzed.py", line 55
except IOError:
^
SyntaxError: invalid syntax
when imported...help?!
Check your indenting. This unhelpful SyntaxError error has fooled me before. :)
From the deleted question:
I'd expect this to be a duplicate, but I couldn't find it.
Here's Python code, expected outcome of which should be obvious:
x = {1: False, 2: True} # no 3
for v in [1,2,3]:
try:
print x[v]
except Exception, e:
print e
continue
I get the following exception: SyntaxError: 'continue' not properly in loop.
I'd like to know how to avoid this error, which doesn't seem to be
explained by the continue documentation.
I'm using Python 2.5.4 and 2.6.1 on Mac OS X, in Django.
Thank you for reading
there's 1 more possible if you're privileged to have an older installation
and
you're using the 'as' syntax:
except IOError as ioe:
and
the parser's getting tripped up on 'as'.
Using as is the preferred syntax in Python 2.6 and better.
It's a syntax error in Python 2.5 and older. For pre-2.6, use this:
except IOError, ioe:
Just missing something in your try block, i.e. pass or anything, otherwise it gives an indentation error.
You will get a syntax error if you don't put something in side the try block.
You can put pass just to hold the space:
try:
# do stuff
# and more stuff
pass
except IOError:
# do this
pass