General wisdom is that if you can perform some kind of pre-validation action to prevent an exception from occurring, you should do that, rather than expect the exception and handle it, as part of “normal” flow control.
However.
Some types of exceptions, especially when related to itneracting with shared/external systems, cannot be conpletely avoided. Checking for the existence of a file is the textbook example of this. No matter how much you check of the existence of the file, it could technically be deleted or exclusively locked by another process before you get a chance to actually open it.
For all intents and purposes, that’s not really likely to happen, so by all means, check for the file, to keep your code sensible, but make sure you have a general strategy for exception handling in place as well.
General wisdom is that if you can perform some kind of pre-validation action to prevent an exception from occurring, you should do that, rather than expect the exception and handle it, as part of “normal” flow control.
However.
Some types of exceptions, especially when related to itneracting with shared/external systems, cannot be conpletely avoided. Checking for the existence of a file is the textbook example of this. No matter how much you check of the existence of the file, it could technically be deleted or exclusively locked by another process before you get a chance to actually open it.
For all intents and purposes, that’s not really likely to happen, so by all means, check for the file, to keep your code sensible, but make sure you have a general strategy for exception handling in place as well.