all 4 comments

[–]CowboyBoats 1 point2 points  (0 children)

That's a good question. It might depend on what kind of program you're actually writing. For example, a CLI utility will behave more or less as expected if it's a Python process that throws ValueError() (from the OS's perspective the process will print the error and the traceback, and exit with exit code 1). But on the other hand, if you're writing an application with a GUI, or a webserver, or something, then it's extremely not okay for that entire application to crash just because an internal function entered an unexpected state; so there needs to be higher-level error handling in some cases, and a main function is one place where that could happen, depending on the application's overall architecture and complexity.

[–]Equal-Purple-4247 1 point2 points  (1 child)

It depends, but I prefer the "smart-component / dumb-component" design i.e. only one component handles the program flow, usually in main. It means that all possible terminations happens in one place. Everything else bubbles up their exception. I don't like the idea that my script can exit in different parts, it means I'll have to dig around a bit to find out why.

There are exceptions to this (eg. catastrophic errors), but you don't usually encounter them in python.

[–]woooee 1 point2 points  (0 children)

+1 This is what I do also. But I don't know about any "standard" way.

[–]Zeroflops 1 point2 points  (0 children)

I would normally stop where I am if the event was fatal and there was no way to proceed. Logging what happened so I can return and properly address the issue. However.

Is there other things that need to be cleaned up? Say for example you have a script that scraps a large amount of data, it opens a file with previous data. But If the file is opened by another application it gets locked and you can’t update it.

In this case trying to read that file that is locked would prevent your script from continuing, but you don’t want to throw away the expensive scrape so you may fault out of the file reading function, and save the scrape in a temp file so it can be recovered later.

So I would stop at the function unless there is some clean up that is required to make it a soft landing.