Manually raising (throwing) an exception in Python
How do I manually throw/raise an exception in Python?
Use the most specific Exception constructor that semantically fits your issue.
Be specific in your message, e.g.:
raise ValueError('A very specific bad thing happened.')
Don't raise generic exceptions
Avoid raising a generic Exception
. To catch it, you'll have to catch all other more specific exceptions that subclass it.
Problem 1: Hiding bugs
raise Exception('I know Python!') # Don't! If you catch, likely to hide bugs.
For example:
def demo_bad_catch():
try:
raise ValueError('Represents a hidden bug, do not catch this')
raise Exception('This is the exception you expect to handle')
except Exception as error:
print('Caught this error: ' + repr(error))
>>> demo_bad_catch()
Caught this error: ValueError('Represents a hidden bug, do not catch this',)
Problem 2: Won't catch
And more specific catches won't catch the general exception:
def demo_no_catch():
try:
raise Exception('general exceptions not caught by specific handling')
except ValueError as e:
print('we will not catch exception: Exception')
>>> demo_no_catch()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
Best Practices: raise
statement
Instead, use the most specific Exception constructor that semantically fits your issue.
raise ValueError('A very specific bad thing happened')
which also handily allows an arbitrary number of arguments to be passed to the constructor:
raise ValueError('A very specific bad thing happened', 'foo', 'bar', 'baz')
These arguments are accessed by the args
attribute on the Exception
object. For example:
try:
some_code_that_may_raise_our_value_error()
except ValueError as err:
print(err.args)
prints
('message', 'foo', 'bar', 'baz')
In Python 2.5, an actual message
attribute was added to BaseException
in favor of encouraging users to subclass Exceptions and stop using args
, but the introduction of message
and the original deprecation of args has been retracted.
Best Practices: except
clause
When inside an except clause, you might want to, for example, log that a specific type of error happened, and then re-raise. The best way to do this while preserving the stack trace is to use a bare raise statement. For example:
logger = logging.getLogger(__name__)
try:
do_something_in_app_that_breaks_easily()
except AppError as error:
logger.error(error)
raise # just this!
# raise AppError # Don't do this, you'll lose the stack trace!
Don't modify your errors... but if you insist.
You can preserve the stacktrace (and error value) with sys.exc_info()
, but this is way more error prone and has compatibility problems between Python 2 and 3, prefer to use a bare raise
to re-raise.
To explain - the sys.exc_info()
returns the type, value, and traceback.
type, value, traceback = sys.exc_info()
This is the syntax in Python 2 - note this is not compatible with Python 3:
raise AppError, error, sys.exc_info()[2] # avoid this.
# Equivalently, as error *is* the second object:
raise sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]
If you want to, you can modify what happens with your new raise - e.g. setting new args
for the instance:
def error():
raise ValueError('oops!')
def catch_error_modify_message():
try:
error()
except ValueError:
error_type, error_instance, traceback = sys.exc_info()
error_instance.args = (error_instance.args[0] + ' <modification>',)
raise error_type, error_instance, traceback
And we have preserved the whole traceback while modifying the args. Note that this is not a best practice and it is invalid syntax in Python 3 (making keeping compatibility much harder to work around).
>>> catch_error_modify_message()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in catch_error_modify_message
File "<stdin>", line 2, in error
ValueError: oops! <modification>
In Python 3:
raise error.with_traceback(sys.exc_info()[2])
Again: avoid manually manipulating tracebacks. It's less efficient and more error prone. And if you're using threading and sys.exc_info
you may even get the wrong traceback (especially if you're using exception handling for control flow - which I'd personally tend to avoid.)
Python 3, Exception chaining
In Python 3, you can chain Exceptions, which preserve tracebacks:
raise RuntimeError('specific message') from error
Be aware:
- this does allow changing the error type raised, and
- this is not compatible with Python 2.
Deprecated Methods:
These can easily hide and even get into production code. You want to raise an exception, and doing them will raise an exception, but not the one intended!
Valid in Python 2, but not in Python 3 is the following:
raise ValueError, 'message' # Don't do this, it's deprecated!
Only valid in much older versions of Python (2.4 and lower), you may still see people raising strings:
raise 'message' # really really wrong. don't do this.
In all modern versions, this will actually raise a TypeError
, because you're not raising a BaseException
type. If you're not checking for the right exception and don't have a reviewer that's aware of the issue, it could get into production.
Example Usage
I raise Exceptions to warn consumers of my API if they're using it incorrectly:
def api_func(foo):
'''foo should be either 'baz' or 'bar'. returns something very useful.'''
if foo not in _ALLOWED_ARGS:
raise ValueError('{foo} wrong, use "baz" or "bar"'.format(foo=repr(foo)))
Create your own error types when apropos
"I want to make an error on purpose, so that it would go into the except"
You can create your own error types, if you want to indicate something specific is wrong with your application, just subclass the appropriate point in the exception hierarchy:
class MyAppLookupError(LookupError):
'''raise this when there's a lookup error for my app'''
and usage:
if important_key not in resource_dict and not ok_to_be_missing:
raise MyAppLookupError('resource is missing, and that is not ok.')
deliberately Raise and throw an exception in python
I'm guessing this is a simplified version of your actual use case, in which case it is generally correct. A couple of notes:
- You can use
raise
without anything after it to re-raise the same error.
try:
raise ValueError('message')
except ValueError:
run_something()
raise # will raise ValueError ('message')
- Don't raise a general
Exception
and don't catch them either, be specific, otherwise this code will obscure other errors and will be difficult to debug. If nothing else suits you, make an exception of your own:
class MyException(Exception):
pass
Then you can use it:
raise MyException('my message')
In your use case, if I understood it correctly, all of this together would be:
class InvalidUsername(Exception):
pass
try:
username = input('username: ')
if len(username) > 6:
raise InvalidUsername('Username is too long')
if '!' in username:
raise InvalidUsername('Invalid character in username')
except InvalidUsername:
handle_invalid_user()
raise
Example in the console:
>>> try:
... username = input('username: ')
... if len(username) > 6:
... raise InvalidUsername('Username is too long')
... if '!' in username:
... raise InvalidUsername('Invalid character in username')
... except InvalidUsername:
... handle_invalid_user()
... raise
...
username: test1234
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
__main__.InvalidUsername: Username is too long
Or with an invalid character:
username: ofer!
Traceback (most recent call last):
File "<stdin>", line 6, in <module>
__main__.InvalidUsername: Invalid character in username
How to use raise keyword in Python
It has 2 purposes.
jackcogdill has given the first one.
It's used for raising your own errors.
if something:
raise Exception('My error!')
The second is to reraise the current exception in an exception handler, so that it can be handled further up the call stack.
try:
generate_exception()
except SomeException as e:
if not can_handle(e):
raise
handle_exception(e)
python how to re-raise an exception which is already caught?
What you're trying to do won't work. Once you handle an exception (without re-raising it), the exception, and the accompanying state, is cleared, so there's no way to access it. If you want the exception to stay alive, you have to either not handle it, or keep it alive manually.
This isn't that easy to find in the docs (the underlying implementation details about CPython are a bit easier, but ideally we want to know what Python the language defines), but it's there, buried in the except
reference:
… This means the exception must be assigned to a different name to be able to refer to it after the except clause. Exceptions are cleared because with the traceback attached to them, they form a reference cycle with the stack frame, keeping all locals in that frame alive until the next garbage collection occurs.
Before an except clause’s suite is executed, details about the exception are stored in the
sys
module and can be accessed viasys.exc_info()
.sys.exc_info()
returns a 3-tuple consisting of the exception class, the exception instance and a traceback object (see section The standard type hierarchy) identifying the point in the program where the exception occurred.sys.exc_info()
values are restored to their previous values (before the call) when returning from a function that handled an exception.
Also, this is really the point of exception handlers: when a function handles an exception, to the world outside that function, it looks like no exception happened. This is even more important in Python than in many other languages, because Python uses exceptions so promiscuously—every for
loop, every hasattr
call, etc. is raising and handling an exception, and you don't want to see them.
So, the simplest way to do this is to just change the workers to not handle the exceptions (or to log and then re-raise them, or whatever), and let exception handling work the way it's meant to.
There are a few cases where you can't do this. For example, if your actual code is running the workers in background threads, the caller won't see the exception. In that case, you need to pass it back manually. For a simple example, let's change the API of your worker functions to return a value and an exception:
def worker(a):
try:
return 1 / a, None
except ZeroDivisionError as e:
return None, e
def master():
res, e = worker(0)
if e:
print(e)
raise e
Obviously you can extend this farther to return the whole exc_info
triple, or whatever else you want; I'm just keeping this as simple as possible for the example.
If you look inside the covers of things like concurrent.futures
, this is how they handle passing exceptions from tasks running on a thread or process pool back to the parent (e.g., when you wait on a Future
).
If you can't modify the workers, you're basically out of luck. Sure, you could write some horrible code to patch the workers at runtime (by using inspect
to get their source and then using ast
to parse, transform, and re-compile it, or by diving right down into the bytecode), but this is almost never going to be a good idea for any kind of production code.
Can I raise an exception if a statement is False?
To raise an exception, you need to use the raise
keyword. I suggest you read some more about exceptions in the manual. Assuming my_function()
sometimes throws IndexError
, use:
try:
content = my_function()
if 'stuff' not in content:
raise ValueError('stuff is not in content')
except (ValueError, IndexError):
exit('Could not complete request.')
Also, you should never use just except
as it will catch more than you intend. It will, for example, catch MemoryError
, KeyboardInterrupt
and SystemExit
. It will make your program harder to kill (Ctrl+C won't do what it's supposed to), error prone on low-memory conditions, and sys.exit()
won't work as intended.
UPDATE: You should also not catch just Exception
but a more specific type of exception. SyntaxError
also inherits from Exception
. That means that any syntax errors you have in your files will be caught and not reported properly.
In python 3.6, how do I catch an exception and raise an exception to be handled later?
You can't. Once you've caught an exception, you can't transfer control to another except
block in the same try
statement. You can use a nested statement:
try:
try:
foobar(-1)
except FooError:
raise BarError
except BarError:
print('Uh oh, bar error!')
sys.exit()
Some additional work is necessary if you want to distinguish between BarError
s raised directly by foobar
and the BarError
raised as a result of the FooError
being caught. You can use exception chaining for this. See PEP-3134 for more details; this example may not be the best way to write this.
try:
try:
foobar(-1)
except FooError as exc:
raise BarError from exc
except BarError as exc:
if isinstance(exc.__cause__, FooError):
print("Caught a Foo-induced BarError")
else:
print("Caught a regular BarError")
Related Topics
Difference Between Numpy.Array Shape (R, 1) and (R,)
How to Programmatically Set an Attribute
Path Issue With Pytest 'Importerror: No Module Named Yadayadayada'
Difference Between Null=True and Blank=True in Django
How to Improve Performance of This Code
How to Remove a Trailing Newline
How to Sort a List of Objects Based on an Attribute of the Objects
How to Use Glob() to Find Files Recursively
How to Schedule Updates (F/E, to Update a Clock) in Tkinter
Add Scrolling to a Platformer in Pygame
How to Change the Order of Dataframe Columns
How to Add New Keys to a Dictionary
How to Write the Fibonacci Sequence
How to Select a Drop-Down Menu Value With Selenium Using Python
Remove All Whitespace in a String
What Is the Python Keyword "With" Used For