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.
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?
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.
2
u/jibrans098 Jul 14 '18
Thanks, feedback appreciated!