As you can see by reading the end of this article , the two main arguments are performance and integrity .
Performance
The first and most obvious reason for not using a try/catch
in the entire program is that, generating a exception
is much more costly than controlling the work flow of the program with if/else
.
In case no exception is thrown, if/else
actually slows down, if a single exception is thrown, the execution time spent treating this exception is equivalent to several if/else
blocks.
Integrity
The second reason, a little less obvious, is that if you've done anything before the exception is thrown, everything that happens until you reach the exception gets executed, which makes execution part of the useless code.
In the meantime, in% w /% these lines that would be useless are ignored before they are executed.
Other issues
In addition to these two problems, in this answer he still addresses the semantic question. Exceptions are for exceptional cases, so they should be used when you do not wait for an error to happen there. While if/else
should be used to control the flow of the program, making it even more natural to read and keep the code easier.
A good example of when we should use if/else
is when we are trying to load a file, faults outside your program may occur, such as that the file no longer exists or there is a problem reading the storage drive, in those cases where success is expected and you can not predict the error is where try/catch
should be used.