0

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?

3
  • 2
    pass Is 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 the pass statement? Commented May 15, 2019 at 14:46
  • 1
    You can remove the pass statement. Commented May 15, 2019 at 14:48
  • What you want is Visual Basic's 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.) Commented May 15, 2019 at 14:53

1 Answer 1

5

When an exception occurs, the code immediately jumps to the exception handler and resumes the code after the exception. It doesn't go back and continue where it left off in the code.

To fix, you have to limit the try/except around the line of code whose exception is to be ignored.

Sign up to request clarification or add additional context in comments.

2 Comments

That means, whenever I want to implement the behavior of logging but not stopping, I have to encase the right lines of code and not the global calling functions? That's really tedious! Is there a way to be smart about it?
If you just have a global function that aborts on an error, and you can't change the internals of the function, no. The except has to catch the error at a location that allows the algorithm to continue. For example, a try/except can't encase a for line in file: do_something('line'), it has to encase just do_something('line'). Then if it fails it can still continue with the next line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.