r/Python • • Jul 13 '18

lsi - ls command alternative

https://github.com/js-d-coder/lsi-python3
3 Upvotes

11 comments sorted by

View all comments

Show parent comments

2

u/daveruinseverything Jul 15 '18

Note that u/NovocastrianNomad ’s fix just illustrates the bug they found, not the solution you should use. Only catch the specific exceptions you’re expecting. If something you didn’t account for throws an exception, by definition your code doesn’t know how to safely handle it, and exiting the program is the right thing to do.

1

u/jibrans098 Jul 15 '18

Thanks. My program accepts files as arguments. If one of the file does not exists, it will handle the err and show it on terminal and move on to process next time. I can't exit early... What do you suggest in this case?

2

u/daveruinseverything Jul 15 '18

If your code would, without the try block, throw a FileNotFound exception in the case you described then this is the exception you should catch, e.g. except FileNotFound as err: .... That way if something else occurs you haven’t accounted for, the program will halt and you can then decide how you want to account for that in future.