Skip to content

Instantly share code, notes, and snippets.

@IQAndreas
Created November 19, 2013 04:24
Show Gist options
  • Select an option

  • Save IQAndreas/7540305 to your computer and use it in GitHub Desktop.

Select an option

Save IQAndreas/7540305 to your computer and use it in GitHub Desktop.
try:
state = "initializing"
read_configs()
state = "downloading"
files = get_files()
state = "importing"
import_files(files)
state = "complete"
exception:
if (state == "importing"):
print("WARNING: The script quit before it finished importing. You may have incomplete files stored on the server")
else:
print("Script quit unexpectedly. Please notify the developer");
@wolever

wolever commented Nov 19, 2013

Copy link
Copy Markdown

I've never seen sys.excepthook used. Generally I'll stick all the code in a top-level function, then wrap the call to that function in try/except:

def main():
    args = parse_args()
    try:
        run(args)
    except MyException:
        print "oh no"

And sometimes I'll have something like:

def run(args):
    try:
        return _run(args)
    except MyException:
        return "oh no there was an error"

So that I don't need to have an entire function indented inside a try block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment