You have to define which type of exception you want to catch. So write except Exception as e: instead of except, e: for a general exception.

Other possibility is to write your whole try/except code this way:

try:
    with open(filepath,'rb') as f:
        con.storbinary('STOR '+ filepath, f)
    logger.info('File successfully uploaded to '+ FTPADDR)
except Exception as e:      # works on python 3.x
    logger.error('Failed to upload to ftp: %s', repr(e))

In older versions of Python 2.x, use except Exception, e instead of except Exception as e:

try:
    with open(filepath,'rb') as f:
        con.storbinary('STOR '+ filepath, f)
    logger.info('File successfully uploaded to %s', FTPADDR)
except Exception, e:        # works on python 2.x
    logger.error('Failed to upload to ftp: %s', repr(e))
Answer from eumiro on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.7 documentation
If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops with an error message.
Discussions

How would I get my error message to print before Python errors?
That error is a python2 error. The easiest fix is to use python3 instead. More on reddit.com
🌐 r/learnpython
11
7
January 1, 2021
Python Exception Handling: Getting the error message
Alexander Davison is having issues with: How do you get the message of a error message (inside the except block)? I know you can do it in Java like this: More on teamtreehouse.com
🌐 teamtreehouse.com
1
December 24, 2016
Capturing Full Exception Message?
Would be extremely helpful if I could somehow also capture the full error message I would get in the terminal (with info on which line caused the issue and similar). Sounds like you want to be printing the traceback then. https://docs.python.org/3/library/traceback.html There's a std library module to help with that, and includes several helper methods to pretty print them More on reddit.com
🌐 r/learnpython
2
1
July 8, 2021
Print exception notes - in repr(exc) or otherwise - Ideas - Discussions on Python.org
Exception notes are printed in tracebacks, but they are omitted when an exception is handled and logged or printed. I think a possibly helpful information is lost. # Python 3.11+ enote = ValueError("error message") enote.add_note("additional info") try: raise enote except Exception as exc: ... More on discuss.python.org
🌐 discuss.python.org
0
September 4, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-print-exception
Python Print Exception - GeeksforGeeks
July 23, 2025 - If the input is 0, a ZeroDivisionError occurs since division by zero isn’t allowed and that error message is printed. This method helps us print the type of exception you caught, like <class 'ValueError'>. It's useful when we're not sure what kind of error occurred. Along with type(e), we can also print e to get the actual error message.
🌐
Esri Community
community.esri.com › t5 › python-questions › what-is-best-way-to-get-an-error-message-in-python › td-p › 370869
What is Best Way to Get an Error Message in Python... - Esri Community
December 11, 2021 - Looking at that documentation, one can see that EnvironmentError is "the base class for exceptions that can occur outside the Python system. For environment errors, 2 items are typically returned: errno and strerror. My guess is that the errors you are missing with your original code are environment errors. I am guessing if you trap and handle environment errors separately, your code will work fine. For example, something along the lines of: try: # some code except EnvironmentError, ee: print ee.strerror except Exception, e: print e.message‍‍‍‍‍‍
🌐
Real Python
realpython.com › python-exceptions
Python Exceptions: An Introduction – Real Python
March 18, 2026 - When you now execute this code ... see the message from your except block printed to the console: ... When an exception occurs in a program that runs this function, then the program will continue as well as inform you about the fact that the function call wasn’t successful. What you didn’t get to see was the type of error that Python raised as ...
🌐
Reddit
reddit.com › r/learnpython › how would i get my error message to print before python errors?
r/learnpython on Reddit: How would I get my error message to print before Python errors?
January 1, 2021 -

I am trying to make a pay calculator to calculate overtime, and my calculator is set up like this:

https://hastebin.com/wehuroruzi.py

From line 14 to 19 I state that If the 'pay_rate' input from the user isn't an integer or float point, then send an error message saying that the user must supply a number.

My issue is that Python gives me an error before the message can be sent to the user. The error being that whatever string that the user sent has no definition in the code.

Find elsewhere
🌐
Coursera
coursera.org › tutorials › how to catch, raise, and print a python exception
How to Catch, Raise, and Print a Python Exception | Coursera
August 13, 2024 - You can use try and except in Python to catch exceptions. Another way to stay current on Python releases and tips is to get involved with the Python community.
🌐
Reddit
reddit.com › r/learnpython › capturing full exception message?
r/learnpython on Reddit: Capturing Full Exception Message?
July 8, 2021 -

So I'm having a script that is running on a schedule from a virtual machine at odd times of the day. Have a weird issue with it that when I run it manually it works perfectly fine, but it breaks due to KeyError when it runs automatically.

It's closely tied to the time zones and processing data from various time zones and bringing them in sync, so it's not unexpected that I have issue on it appearing when it runs at midnight (in relevant time zones).

However, I need to see which data source is breaking exactly and I'm having trouble doing so. I attempted to capture the error using try/except and write it to a txt file in this way:

try:

# code lives here

except Exception as e

#opening the file

f.write(str(e))

However, this only logs the key that wasn't found and as luck would have it, it means I have a minimum of three places to look for that. Would be extremely helpful if I could somehow also capture the full error message I would get in the terminal (with info on which line caused the issue and similar).

Thanks in advance!

🌐
Python.org
discuss.python.org › ideas
Print exception notes - in repr(exc) or otherwise - Ideas - Discussions on Python.org
September 4, 2024 - Exception notes are printed in tracebacks, but they are omitted when an exception is handled and logged or printed. I think a possibly helpful information is lost. # Python 3.11+ enote = ValueError("error message") enote.add_note("additional info") try: raise enote except Exception as exc: ...
🌐
Python
docs.python.org › 3 › builtins › exceptions.html
Built-in Exceptions — Python 3.14.7 documentation
Note that because of the underlying memory management architecture (C’s malloc() function), the interpreter may not always be able to completely recover from this situation; it nevertheless raises an exception so that a stack traceback can be printed, in case a run-away program was the cause. ... Raised when a local or global name is not found. This applies only to unqualified names. The associated value is an error message that includes the name that could not be found.
🌐
Real Python
realpython.com › python-traceback
Understanding the Python Traceback – Real Python
July 29, 2019 - In this case, the error message line will read, AttributeError: 'NoneType' object has no attribute 'append'. The ImportError is raised when something goes wrong with an import statement. You’ll get this exception, or its subclass ModuleNotFoundError, if the module you are trying to import can’t be found or if you try to import something from a module that doesn’t exist in the module. The Python documentation defines when this exception is raised:
🌐
UiPath Community
forum.uipath.com › help › activities
Exception - Get Python Object - Activities - UiPath Community Forum
March 13, 2021 - Anyone here know how to increase the size quota of incoming messages? I am getting the following error when I am trying to retrieve the python object as a string after invoking a python method in UiPath Studio. Or anyone here has faced this issue and has any workaround?
🌐
DataCamp
datacamp.com › tutorial › exception-handling-python
Exception & Error Handling in Python | Tutorial by DataCamp | DataCamp
May 29, 2026 - Instead of throwing the exception and terminating the program, it will display the error message we provided. value = 2_000 try: if value > 1_000: # raise the ValueError raise ValueError("Please add a value lower than 1,000") else: print("Congratulations! You are the winner!!") # if false then raise the value error except ValueError as e: print(e) This type of exception handling helps us prepare for errors not covered by Python and are specific to your application requirement.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-exception-handling
Python Exception Handling - GeeksforGeeks
The try block contains code that may fail and except block catches the error, printing a safe message instead of stopping the program. Python provides four main keywords for handling exceptions: try, except, else and finally each ...
Published: May 29, 2026
🌐
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.
🌐
EyeHunts
tutorial.eyehunts.com › home › python print exception message
Python Print exception message
December 30, 2022 - Simple example code catch and prints ... 78, 88] for elem in list_arr: try: print("Result: ", elem / 9) except Exception as e: print("Exception occurred for value '" + elem + "': " + repr(e)) ... import logging logger = ...
🌐
JanBask Training
janbasktraining.com › community › python-python › how-to-log-the-exception-message-using-by-logging-module
How to log the Exception message using by logging module? | JanBask Training Community
July 22, 2024 - How can I modify the code to log ... the exceptions and log their message in Python programming language, you can use the logging module....