How can you tell by reading the end of that Article, the two main arguments are performance and integrity.
Performance
The first and most obvious reason not to use a try/catch
in the entire program is that, generate a exception
is much more expensive than controlling the work flow of the program with if/else
.
In case no exception is made, the if/else
actually makes the flow slower, incoming, if a single exception is thrown, the running time spent to treat this exception is equivalent to several blocks if/else
.
Integrity
The second reason, a little less obvious, is that if you have done anything before the exception is launched, everything that happens until you arrive reach the exception is executed, which makes the execution of part of the code useless.
Meanwhile, in the if/else
those lines that would be useless are despised before being executed.
Other issues
Beyond these two problems, in that reply it still deals with the semantic issue. Exceptions are for exceptional cases, so should be used when you waiting that a mistake will happen there. While if/else
should be used to control the flow of the programme, including making it more natural to read and maintain the code easier.
A good example of when we should use try/catch
is when we are trying to upload a file, there may be faults unrelated to your program, such as for example the file no longer exists or some problem occurs in reading the storage drive, in such cases where success is expected and you there’s no way to predict the error is where the try/catch
must be used.
Never do this, will give a slow that you can go to the moon and come back and the program has not finished its tasks.
– ptkato
That answer might help you What Try/Catch Blocks are for and when they should be used?
– DNick