148
votes
3answers
65k views

How do I manually throw/raise an exception in python?

I want to make an error on purpose, so that it would go into the except: How do I do that?
143
votes
4answers
41k views

Proper way to declare custom exceptions in modern Python?

What's the proper way to declare custom exception classes in modern Python? My primary goal is to follow whatever standard other exception classes have, so that (for instance) any extra string I ...
115
votes
5answers
70k views

How do I check if a variable exists in Python?

I want to check if a variable exists. Now I'm doing something like this: try: myVar except NameError: # Doint smth Are there any other ways without exceptions? Or is that part of code right?
70
votes
7answers
163k views

Try/Catch in Python: How to properly ignore Exceptions?

When you just want to do a try catch without handling the exception, how do you do it in Python? Is the following the right way to do it? try : shutil.rmtree ( path ) except : pass
66
votes
10answers
19k views

How do you test that a Python function throws an exception?

How does one write a test that fails only if a function doesn't throw an expected exception?
65
votes
4answers
13k views

Which exception should I raise on bad/illegal argument combinations in Python?

I was wondering about the best practices for indicating invalid argument combinations in Python. I've come across a few situations where you have a function like so: def import_to_orm(name, ...
57
votes
4answers
34k views

Python: How to ignore an exception and proceed?

I have a try...except block in my code and When an exception is throw. I really just want to continue with the code because in that case, everything is still able to run just fine. The problem is if ...
56
votes
4answers
22k views

BaseException.message deprecated in Python 2.6

I get a warning that BaseException.message is deprecated in Python 2.6 when I use the following user-defined exception: class MyException(Exception): def __init__(self, message): self.message = ...
41
votes
10answers
3k views

How are exceptions implemented under the hood?

Just about everyone uses them, but many, including me simply take it for granted that they just work. I am looking for high-quality material. Languages I use are: Java, C, C#, Python, C++, so these ...
40
votes
4answers
19k 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 ...
37
votes
6answers
14k 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 ...
32
votes
8answers
5k views

“Inner exception” (with traceback) in Python?

My background is in C# and I've just recently started programming in Python. When an exception is thrown I typically want to wrap it in another exception that adds more information, while still ...
29
votes
3answers
9k views

In Python, what's the difference between 'except Exception as e' and 'except Exception, e'

In python, there are two ways to catch an exception except Exception, e: except Exception as e: It seems like 'as e' is the one to use going forward. In what version of python did this change? ...
26
votes
4answers
5k views

Break on exception in pydev

Is it possible to get the pydev debugger to break on exception?
23
votes
2answers
8k views

What does raise in Python raise?

Consider the following code: try: raise Exception("a") except: try: raise Exception("b") finally: raise This will raise Exception: a. I expected it to raise Exception: b ...

1 2 3 4 5 51
15 30 50 per page