r/AskProgramming • u/Excellent-Fan8457 • Jul 11 '26
When to use try/except vs if/else
I was making a simple program when I wondered when to use try and except this is the code I'm looking at.
if (Path.home() / "Desktop").exists():
desktop_path = Path.home() / "Desktop"
else:
desktop_path = Path.home() / "OneDrive/Desktop"
4
Upvotes
View all comments
Show parent comment
1
u/_abscessedwound Jul 11 '26
If we’re talking about python language specifics, like FileNotFoundError, that’s very different than exceptions generally.
Python’s philosophy for error-handling tends to favour explicit and uniform error handling over having well-defined domains for unexpected and unrecoverable states, failure states, and recoverable states.
I personally disagree that all of the above three should be in the same bin, but I respect the design choice: it’s a lot easier to learn.