Tagged Questions
32
votes
6answers
13k views
Catch a thread's exception in the caller thread in Python
I'm very new to Python and multithreaded programming in general. Basically, I have a script that will copy files to another location. I would like this to be placed in another thread so I can output ...
22
votes
2answers
3k views
Try/catch or validation for speed?
I'm working with Python and whenever I've had to validate function input, I assumed that the input worked, and then caught errors.
In my case, I had a universal Vector() class which I used for a few ...
19
votes
6answers
5k views
is there a pythonic way to try something up to a maximum number of times?
I have a python script which is querying a MySQL server on a shared linux host. For some reason, queries to MySQL often return a "server has gone away" error:
_mysql_exceptions.OperationalError: ...
11
votes
4answers
3k views
How can I handle exceptions in a list comprehension in Python?
I have some a list comprehension in Python in which each iteration can throw an exception.
For instance, if I have:
eggs = (1,3,0,3,2)
[1/egg for egg in eggs]
I'll get a ZeroDivisionError ...
40
votes
4answers
18k views
Python When I catch an exception, how do I get the type, file, and line number?
Catching an exception that would print like this:
Traceback (most recent call last):
File "c:/tmp.py", line 1, in <module>
4 / 0
ZeroDivisionError: integer division or modulo by zero
I ...
12
votes
8answers
4k views
Cheap exception handling in Python?
I read in an earlier answer that exception handling is cheap in Python so we shouldn't do pre-conditional checking.
I have not heard of this before, but I'm relatively new to Python. Exception ...
5
votes
7answers
512 views
Handle generator exceptions in its consumer
This is a follow-up to Handle an exception thrown in a generator and discusses a more general problem.
I have a function that reads data in different formats. All formats are line- or record-oriented ...
232
votes
1answer
24k views
Catch multiple exceptions in one line (except block)
I know that I can do:
try:
# do something that may fail
except:
# do this if ANYTHING goes wrong
I can also do this:
try:
# do something that may fail
except ...
4
votes
2answers
781 views
Best Practices for Python UnicodeDecodeError
I use Pylons framework, Mako template for a web based application. I wasn't really bother too deep into the way Python handles the unicode strings. I had tense moment when I did see my site crash when ...
6
votes
5answers
2k views
Python Global Exception Handling
So I want to catch KeyboardInterrupt globally, and deal with it nicely. I don't want to encase my entire script in a huge try/except statement, because that just sounds gross. Is there any way to do ...
38
votes
4answers
9k views
Catching an exception while using a Python 'with' statement
To my shame, I can't figure out how to handle exception for python 'with' statement. If I have a code:
with open("a.txt") as f:
print f.readlines()
I really want to handle 'file not found ...
24
votes
6answers
18k views
Print the full traceback in python (without halting the program)
I'm writing a program that parses a 10 websites, locates data files, saves the files, and then parses them to make data that can be readily used in numpy. There are TONS of errors this file encounters ...
17
votes
11answers
7k views
hasattr() vs try-except block to deal with non-existent attributes
if hasattr(obj, 'attribute'):
# do somthing
vs
try:
# access obj.attribute
except AttributeError, e:
# deal with AttributeError
Which should be preferred and why?
12
votes
5answers
1k views
Better to 'try' something and catch the exception or test if its possible first to avoid an exception?
Should I test if something is valid or just try to do it and catch the exception?
Is there any solid documentation saying that one way is preferred?
Is one way more pythonic?
For example, should ...
4
votes
4answers
203 views
Does a exception with just a raise have any use?
For example, here is some code from django.templates.loader.app_directories.py.[1]
try:
yield safe_join(template_dir, template_name)
except UnicodeDecodeError:
# The template dir name was a ...