Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • 1
    The one that happens first. Commented Jul 28, 2014 at 18:37
  • The one you were catching ? Commented Jul 28, 2014 at 18:38
  • 1
    This is exactly why (a) you want to write exception handlers that either can't raise, or can only raise in specific situations that you understand very well, and (b) you want to actually get the exception object, at least during early debugging, so you can, e.g., print('do_something_dangerous raised {!r}'.format(e)) at least level and see what's going on. Commented Jul 28, 2014 at 18:42
  • 1
    Also, notice that in Ned's "real" code he handles only except Exception, not just bare except:. Partly this is to allow him to capture the exception with except Exception, e: (although this is an old blog, so it uses old syntax; you want except Exception as e:), and partly to avoid catching things like KeyboardInterrupt, which you very rarely want to handle. (When you do, make it explicit with except BaseException:. That wasn't an option in older Python, but assuming you're writing for, e.g., 2.6 or 3.0, it is.) Commented Jul 28, 2014 at 18:45
  • 1
    @user2815780: For that case, you probably want to handle the apologize code in some way, so you can raise an exception that lets you code/the end user know that you failed and then failed to clean up. (Or maybe create the folder in a temporary location on the destination drive/filesystem so the worst-case scenario isn't as bad and maybe doesn't have to be reported.) But otherwise, yeah, that's a perfectly reasonable use to exception-handling code that may raises. Commented Jul 28, 2014 at 19:18