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.
N Randhawa
Is this you? As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.
Claim your profile
Get in touch with N
Contact N, search articles and posts on X, monitor coverage, and track replies from one place.
Learn more about Muck Rack