Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try/Catch or IF for handling missing Files?

Is it better to try/catch exceptions or to use if statements to handle the different outcomes?

I am writing a short program in Java to copy files for convenience, and use the ifs to handle the event where a file does not exist, and declare the throws for each method, but don't use try/catch.

Should I go back and replace those ifs and the related sections with try/catches or is this "acceptable" programming" for sharing with a small community of users?

like image 516
Klein Avatar asked Dec 28 '22 20:12

Klein


1 Answers

If by missing, you mean the file should always be there, then that's an exception.

If you mean that a missing file is an ordinary (possible or even likely) occurance, then do not use an exception.

like image 160
Mitch Wheat Avatar answered Jan 17 '23 14:01

Mitch Wheat