Showing results for python try except raise
Search instead for python tryexcept raise
You would never do this. You might catch something to log it or clean up some stuff, then reraise so it can be handled somewhere else… but you only want to catch exceptions in a function that you want to keep going. Most of the time when an exception happens in a function, you don’t want it to contrite, so I just let the exception happen and catch it higher up. Most of my function code will do checks and just raise an exception if needed, but doesn’t handle them locally. There are only a few key places that I handle those exceptions so the whole program won’t crash. It seems like a lot of new programmers try/except in a lot of places that it’s not necessary. Or they will return a different value if there was an exception which creates other value checks instead of just raising the exception. Answer from cointoss3 on reddit.com
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.3 documentation
The variable is bound to the exception instance which typically has an args attribute that stores the arguments. For convenience, builtin exception types define __str__() to print all the arguments without explicitly accessing .args. >>> try: ... raise Exception('spam', 'eggs') ...
🌐
W3Schools
w3schools.com › python › python_try_except.asp
Python Try Except
Print one message if the try block raises a NameError and another for other errors: try: print(x) except NameError: print("Variable x is not defined") except: print("Something else went wrong") Try it Yourself » · See more Error types in our Python Built-in Exceptions Reference.
🌐
Reddit
reddit.com › r/learnpython › what's the point of try/except just to raise the exception?
r/learnpython on Reddit: What's the point of try/except just to raise the exception?
July 25, 2025 -

For context, I'm primarily a database guy but have been using Python a lot lately. I know enough to figure out how to do most things I want to do, but sometimes lack the context of why certain patterns are used/preferred.

Looking through some of the code the software engineers at my organization have written in Python, they make use of try/except blocks frequently and I generally understand why. However, they're often writing except blocks that do nothing but raise the exception. For example:

def main() -> None:  
  try:
    run_etl()
  except Exception as err:
    raise err

Sometimes (not always), I'll at least see logger.error(f"Encountered an exception: {err} before they raise the exception (I have no idea why they're not using logger.exception). Still, since we just let the logging module write to sys.stderr I don't know what we're really gaining.

What is the point of wrapping something in a try/except block when the only thing we're doing is raising the exception? I would understand if we were trying to handle exceptions so the program could continue or if we made use of a finally block to do some sort of post-error cleanup, but we're not. It seems to me like we're just catching the error to raise it, when we could have just let the error get raised directly.

TIA!

Top answer
1 of 21
5
What's the point of try/except just to raise the exception? You are correct. This is pointless, and worse, it obfuscates what the code is doing and makes it harder to maintain. I suspect that what may have happened in some of these places is that it used to do something else in the except block, and that got removed without cleaning up the surrounding code. Another possibility is that some of these are the result of someone blindly copying and pasting code they saw elsewhere. This type of thing happens a lot. Sometimes (not always), I'll at least see logger.error(f"Encountered an exception: {err} before they raise the exception (I have no idea why they're not using logger.exception). Still, since we just let the logging module write to sys.stderr I don't know what we're really gaining. That's pretty common. In production you'd run your code in an environment that captures stderr and stdout, parses them, and then indexes them into a logging system (eg: DataDog or Prometheus). Edit: removed bit about printf debugging, as that isn't what the code in question appears to be doing. (Didn't look closely enough earlier, when I was on my phone.)
2 of 21
5
I will often do this by default until I figure out later how I want to handle specific exception types that can happen as a result of specific situations, because thinking about exception handling can distract me from what I am actually trying to accomplish. It can also help with debugging, because you can put print or log statements in the except section in places you think are relevant and narrow down which one causes the problem. Also, enterprises can have standards that any call that could create an exception should each have its own try except, and you often don't need to do error handling more granularly than the function level, so sometimes you just do this. I think in general it is better practice to have more try excepts than fewer, and I rarely feel it can get excessive
🌐
Coursera
coursera.org › tutorials › python-exception
How to Catch, Raise, and Print a Python Exception | Coursera
Additionally, handling exceptions this way enables you to replace the interpreter’s error message with a much more user friendly one. The raise statement allows you to force an error to occur.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-try-except
Python Try Except - GeeksforGeeks
July 23, 2025 - In case an error occurs in try-block, Python stops executing try block and jumps to exception block. These blocks let you handle the errors without crashing the program. ... try: # Code that may raise an exception x = 3 / 0 print(x) except: # exception occurs, if code under try throws error print("An exception occurred.")...
🌐
Honeybadger
honeybadger.io › blog › a-guide-to-exception-handling-in-python
The ultimate guide to Python exception handling - Honeybadger Developer Blog
March 28, 2025 - Sometimes, you might want to catch multiple types of exceptions, but you want to treat them the same. You can catch multiple exceptions with identical handling logic. Python lets you group them in a single except clause using parentheses. Check out this example: def fetch_data(source): if source == "file": raise FileNotFoundError("File source missing!") elif source == "api": raise ConnectionError("Failed to connect to API!") else: return "Data fetched successfully!" sources = ["file", "api", "database"] for src in sources: try: data = fetch_data(src) print(data) except (FileNotFoundError, ConnectionError) as e: print("Recoverable error:", e) except Exception as ex: print("Unknown error:", ex)
Top answer
1 of 11
4322

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.')
2 of 11
579

Don't do this. Raising a bare Exception is absolutely not the right thing to do; see Aaron Hall's excellent answer instead.

It can't get much more Pythonic than this:

raise Exception("I know Python!")

Replace Exception with the specific type of exception you want to throw.

See the raise statement documentation for Python if you'd like more information.

Find elsewhere
🌐
Python Land
python.land › home › language deep dives › python try except: examples and best practices
Python Try Except: Examples And Best Practices • Python Land Tutorial
January 29, 2026 - If your knowledge about objects, ... and classes and my article on inheritance first. When something unexpected occurs, we can raise an exception at the point of the error....
🌐
Pylint
pylint.readthedocs.io › en › latest › user_guide › messages › warning › try-except-raise.html
try-except-raise / W0706 - Pylint 4.1.0-dev0 documentation
def execute_calculation(a, b): try: return some_calculation(a, b) except ZeroDivisionError: raise except ArithmeticError: return float('nan')
🌐
Sentry
sentry.io › sentry answers › python › raise an exception in python
Raise an exception in Python | Sentry
June 15, 2023 - In addition to the error message provided in the first parameter, the Exception constructor can be given any number of additional values as arguments. It will store these in a tuple called args. try: raise Exception("My custom exception.", 1, "a", True) except Exception as e: print(e.args) # will print (1, "a", True)
🌐
YouTube
youtube.com › watch
Try Except Raise - Python for Beginners - YouTube
In this video we will cover how to use try except blocks to catch errors before they break a program.Basic Steps:Setup basic variables, inputs.Define try exc...
Published   September 8, 2023
🌐
W3Schools
w3schools.com › python › gloss_python_raise.asp
Python Raise an Exception
x = -1 if x < 0: raise Exception("Sorry, no numbers below zero") Try it Yourself » · The raise keyword is used to raise an exception.
🌐
Real Python
realpython.com › python-exceptions
Python Exceptions: An Introduction – Real Python
December 1, 2024 - With the raise keyword, you can raise any exception object in Python and stop your program when an unwanted condition occurs. Before moving on to the most common way of working with exceptions in Python using the try …
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-valueerror-exception-handling-examples
Python ValueError Exception Handling Examples | DigitalOcean
August 3, 2022 - Our program can raise ValueError in int() and math.sqrt() functions. So, we can create a nested try-except block to handle both of them.
🌐
DataCamp
datacamp.com › tutorial › python-try-except
Python Try-Except Tutorial: Best Practices and Real-World Examples | DataCamp
August 26, 2025 - That raise at the end is important, it rethrows the same exception after logging it. Without it, you’ve just swallowed the error. Sometimes that’s fine, but usually, it’s not. Then there's the raise from trick. This one’s for when you’re handling one error but need to raise another, and you don’t want to lose the original one. Python lets you chain them:
🌐
Reddit
reddit.com › r/learnpython › how to raise exceptions in try/except and if/else combination?
r/learnpython on Reddit: How to raise exceptions in try/except and if/else combination?
September 11, 2023 -

I can never seem to wrap my head around try/except and if/else. I've written a custom exception called InvalidInput. Right now, I have my main code written to raise the exception like this:

data = someString.split('.')[0]
try:
   if data == 'blah':
      #do more stuff
   else:
      raise InvalidInput('You have submitted an invalid value.")
except InvalidInput as e:
    InvalidInput(e)

This is a flask app so it routes the exception to this:

@main.app_errorhandler(InvalidInput)
def handle_invalid_input(error):
  response= jsonify(error.to_dict())
  response.status = error.status_code
  return response

The try/except block seems redundant. Do I need it or could I just raise it from the else and be done with it? I do not surround my whole entire code with a try/except, but I assume raising an exception anywhere means it gets caught by default, whether you explicitly have it contained in a try/except block or not. I assume it's bad form though to not use try/except. I've looked at a lot of stackoverflow threads but I rarely see an example like this. Please help me understand :)

🌐
Programiz
programiz.com › python-programming › exception-handling
Python Exception Handling (With Examples)
Here, the assert statement in the code checks that num is an even number; if num is odd, it raises an AssertionError, triggering the except block. Note: Exceptions in the else clause are not handled by the preceding except clauses. In Python, the finally block is always executed no matter whether ...
🌐
Real Python
realpython.com › python-raise-exception
Python's raise: Effectively Raising Exceptions in Your Code – Real Python
January 25, 2025 - You can re-raise an exception by using a bare raise within an except block to preserve the original traceback. Learning about the raise statement will allow you to handle errors and exceptional situations effectively in your code.
Top answer
1 of 3
24

Okey, so there a few things that need to be explained where.

What is try-except used for?

It is used for catching errors raised by the program. Any code susceptible of raising an exception is inserted inside a try statement, and below that statement, any number of except statements with any single error that you want to catch.

try:
    user_input = int(input('Give me a number: '))
except ValueError:
    print('That is not a number!')

When should i use try-except?

It is not a good practice to use a try-except on every single line of code that could raise an error, because that may be half of it, or more. So when shall you use it? Simple, ask this question: Do I want to do any custom action with that error being raised? If the answer is yes, you are good to go.

Catching Exception or empty except

As I see in your example, you are using an empty except. Using an empty except statement will catch every single error raised that the surrounded code, which is similar (but not the same) as catching Exception. The Exception class is the superclass of every single built-in exception in the Python environment that are non-system-exiting (read here) and its generally a bad practice to catch either all exceptions with except: or Exception with except Exception:. Why? Because you are not letting the user (or even you, the programmer) know what error you are handling. For example:

fruits = ['apple', 'pear', 'banana']
try: 
    selection = fruits[int(input('Select a fruit number (0-2): '))]  
except Exception:
    print('Error!')
    # But wait, are you catching ValueError because the user did not input a number, 
    # or are you catching IndexError because he selected an out of bound array index? 
    # You don't know  

Catching multiple exceptions

Based on the previous example, you can use multiple try-except statements to difference which errors are being raised.

fruits = ['apple', 'pear', 'banana']
try: 
    selection = fruits[int(input('Select a fruit number (0-2): '))]  
except ValueError:
    print('That is not a number')
except IndexError:
    print('That fruit number does not exist!')  

Grouping exceptions

If there are two particular exceptions that you want to use for a same purpose, you can group them in a tuple:

fruits = ['apple', 'pear', 'banana']
try: 
    selection = fruits[int(input('Select a fruit number (0-2): '))]  
except (ValueError, IndexError):
    print('Invalid selection!')  

Your case

Based on this information, add those try-except blocks to your code, and see what possible errors that could be raised during its execution, asking the previously recommended question Do I want to execute some custom action with this error?

Additionally

  • There are try-except-else statements. See here
  • There are try-except-finally statements. See here
  • You can combine them all in a try-except1-except2...exceptN-else-finally statement.
  • I recommend you get familiar with built-in errors why practicing this!
2 of 3
0
  1. try: code that might cause an error

  2. except: code that runs if an error happens

  3. else: runs if no error happens

  4. finally: always runs (good for cleanup, closing files, etc.)


Example 1: Basic Example
try:
    num = int("abc")   # This will raise an error
    print("Number:", num)
except ValueError:
    print("Oops! Could not convert to int.")


Example 2:

try:
    x = 10 / 0
except ZeroDivisionError:
    print("You cannot divide by zero!")
except ValueError:
    print("Invalid value!")

Example 3:
try:
    x = 5 / 1
except ZeroDivisionError:
    print("Division by zero not allowed.")
else:
    print("Division successful:", x)   # runs if no error
finally:
    print("Always runs, even if there was an error.")

Example 4: General 
try:
    # risky code
    x = 10 / 0
    y = int("abc")
except Exception as e:
    print("Error occurred:", e)
🌐
Python Basics
pythonbasics.org › try-except
Try and Except in Python - Python Tutorial
If you open the Python interactive ... syntax of the try-except block is: ... try: the code with the exception(s) to catch. If an exception is raised, it jumps straight into the except block....