tl;dr I use try: ... except Exception as err: pass but the code still stops when an exception is raised, even if it shouldn't.
I am running a transformation algorithm on a csv. And one of the transformations takes into account the existence of a column's values in a database. If it doesn't exist, I raise an exception like this :
2019-05-15 16:36:37,095 - root - WARNING - The data in 'section_code' either doesn't exist in the raw data or hasn't been correctly encoded. The following strings caused the encoding error : '['398', 'die']'
I know it isn't good to use this practice of try: ... except: pass, but I want my code to continue on transforming the file while only logging the errors/warnings where they appear.
Here's how my code is encased in the try...except block
if __name__ == "__main__":
try:
# Initiate logger
logging.basicConfig(
level="DEBUG",
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Business code ...
except FileNotFoundError as f_err:
logging.exception(f_err)
except AzureMissingResourceHttpError as az_err:
logging.exception(az_err)
except TypeError as t_err:
pass
logging.warning(t_err)
So my question is: why does the pass statement not have the wanted effect, and how do I implement this behavior?
passIs really for place-holder code for when statements are required, but you haven't coded them in yet. If you're just throwing the exception to the logger, why not just remove thepassstatement?on error resume next, and there is no such thing in Python. (Which is a good thing; it forces you to think about exactly where errors may occur, and how they should be handled.)