Timeline for rethrowing python exception. Which to catch?
Current License: CC BY-SA 4.0
Post Revisions
14 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Oct 25, 2022 at 14:01 | answer | added | Karel Marik | timeline score: 14 | |
| S Jul 9, 2021 at 0:50 | history | suggested | bappak | CC BY-SA 4.0 |
Fix grammar, spacing, and markdown
|
| Jul 8, 2021 at 17:47 | review | Suggested edits | |||
| S Jul 9, 2021 at 0:50 | |||||
| Jul 28, 2014 at 19:18 | comment | added | abarnert |
@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.
|
|
| Jul 28, 2014 at 18:51 | vote | accept | Bosiwow | ||
| Jul 28, 2014 at 18:47 | comment | added | Bosiwow | Well I've got following situation. I have to copy from one usb drive to another. At the destination I have to create a folder, where I can copy to. If something goes wrong during the copy process, I want to remove the destination folder if something goes wrong, so I don't have a incomplete destination folder. To remove the folder I have to call a function that removes the folder. But that might cause a new exception, I guess :/ | |
| Jul 28, 2014 at 18:45 | comment | added | abarnert |
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.)
|
|
| Jul 28, 2014 at 18:42 | comment | added | abarnert |
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.
|
|
| Jul 28, 2014 at 18:42 | comment | added | jonrsharpe |
'Since you re-throw the exception, there shouold [sic] be an "outer catch-except"' - not necessarily, some errors should get all the way to the user. 'do_something_to_apologize() inside the except throws an error' - the thing you're doing to recover from errors should generally not be error-prone! Sometimes, of course, it's unavoidable. 'the exception with the highest priotiry [sic]' - how should errors be prioritised? Most standard errors inherit from Exception, there's not much hierarchy. Questions aside, though, why don't you just try it and see?
|
|
| Jul 28, 2014 at 18:40 | answer | added | BrenBarn | timeline score: 100 | |
| Jul 28, 2014 at 18:38 | answer | added | ibebrett | timeline score: 8 | |
| Jul 28, 2014 at 18:38 | comment | added | Bosiwow | The one you were catching ? | |
| Jul 28, 2014 at 18:37 | comment | added | Ismail Badawi | The one that happens first. | |
| Jul 28, 2014 at 18:36 | history | asked | Bosiwow | CC BY-SA 3.0 |