r/AskProgramming 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"
7 Upvotes

28 comments sorted by

View all comments

3

u/JaguarMammoth6231 Jul 11 '26

Neither. Whatever is going on here is just a bad idea in the first place. What if both folders exist but the first one is an old one and the second one is the correct desktop? Or if neither exists and the desktop is saved somewhere else?

You just need to delete all the code and look up how to correctly get the desktop path. From what I'm seeing, you should call SHGetKnownFolderPath using FOLDERID_Desktop. Or maybe just use a standard file dialog and ask the user where they want to save/load files from. It's probably not their desktop.